@orthograph/portal 0.0.0-stage → 0.1.0-next.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.
Files changed (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +24 -2
  3. package/dist/index.d.ts +14 -0
  4. package/dist/index.js +12 -0
  5. package/dist/modules/live/internal/join.d.ts +5 -0
  6. package/dist/modules/live/internal/join.js +51 -0
  7. package/dist/modules/live/service.d.ts +2 -0
  8. package/dist/modules/live/service.js +5 -0
  9. package/dist/modules/live/types.d.ts +60 -0
  10. package/dist/modules/live/types.js +1 -0
  11. package/dist/modules/monitor/internal/evaluate.d.ts +5 -0
  12. package/dist/modules/monitor/internal/evaluate.js +23 -0
  13. package/dist/modules/monitor/internal/issues.d.ts +6 -0
  14. package/dist/modules/monitor/internal/issues.js +54 -0
  15. package/dist/modules/monitor/service.d.ts +2 -0
  16. package/dist/modules/monitor/service.js +19 -0
  17. package/dist/modules/monitor/types.d.ts +54 -0
  18. package/dist/modules/monitor/types.js +1 -0
  19. package/dist/modules/reader/internal/rows.d.ts +20 -0
  20. package/dist/modules/reader/internal/rows.js +21 -0
  21. package/dist/modules/reader/service.d.ts +2 -0
  22. package/dist/modules/reader/service.js +23 -0
  23. package/dist/modules/reader/types.d.ts +50 -0
  24. package/dist/modules/reader/types.js +9 -0
  25. package/dist/modules/server/internal/client.d.ts +3 -0
  26. package/dist/modules/server/internal/client.js +33 -0
  27. package/dist/modules/server/internal/feed.d.ts +15 -0
  28. package/dist/modules/server/internal/feed.js +37 -0
  29. package/dist/modules/server/internal/ledger.d.ts +3 -0
  30. package/dist/modules/server/internal/ledger.js +12 -0
  31. package/dist/modules/server/service.d.ts +2 -0
  32. package/dist/modules/server/service.js +92 -0
  33. package/dist/modules/server/types.d.ts +48 -0
  34. package/dist/modules/server/types.js +1 -0
  35. package/dist/modules/snapshot/internal/judge.d.ts +21 -0
  36. package/dist/modules/snapshot/internal/judge.js +33 -0
  37. package/dist/modules/snapshot/internal/read.d.ts +11 -0
  38. package/dist/modules/snapshot/internal/read.js +27 -0
  39. package/dist/modules/snapshot/service.d.ts +2 -0
  40. package/dist/modules/snapshot/service.js +52 -0
  41. package/dist/modules/snapshot/types.d.ts +64 -0
  42. package/dist/modules/snapshot/types.js +1 -0
  43. package/dist/modules/telemetry/internal/calls.d.ts +23 -0
  44. package/dist/modules/telemetry/internal/calls.js +114 -0
  45. package/dist/modules/telemetry/internal/counts.d.ts +6 -0
  46. package/dist/modules/telemetry/internal/counts.js +56 -0
  47. package/dist/modules/telemetry/internal/failure.d.ts +11 -0
  48. package/dist/modules/telemetry/internal/failure.js +36 -0
  49. package/dist/modules/telemetry/internal/group.d.ts +11 -0
  50. package/dist/modules/telemetry/internal/group.js +40 -0
  51. package/dist/modules/telemetry/internal/spans.d.ts +26 -0
  52. package/dist/modules/telemetry/internal/spans.js +46 -0
  53. package/dist/modules/telemetry/internal/trace.d.ts +2 -0
  54. package/dist/modules/telemetry/internal/trace.js +35 -0
  55. package/dist/modules/telemetry/service.d.ts +2 -0
  56. package/dist/modules/telemetry/service.js +23 -0
  57. package/dist/modules/telemetry/types.d.ts +180 -0
  58. package/dist/modules/telemetry/types.js +2 -0
  59. package/dist/modules/wire/internal/depends.d.ts +3 -0
  60. package/dist/modules/wire/internal/depends.js +17 -0
  61. package/dist/modules/wire/internal/digest.d.ts +3 -0
  62. package/dist/modules/wire/internal/digest.js +6 -0
  63. package/dist/modules/wire/service.d.ts +2 -0
  64. package/dist/modules/wire/service.js +36 -0
  65. package/dist/modules/wire/types.d.ts +552 -0
  66. package/dist/modules/wire/types.js +174 -0
  67. package/dist/server.d.ts +3 -0
  68. package/dist/server.js +4 -0
  69. package/package.json +39 -3
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,25 @@
1
- # Temporary Holding Version
1
+ # @orthograph/portal
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The read-only portal every project gets (layer 2, L2.10; F1, D3 #130 to #138; K19). It has two halves, and only the small one goes into a
4
+ project's Worker.
5
+
6
+ - **The server half, `@orthograph/portal/server`** (`PortalServer`): the data API at `/_portal/v1/*` (the blueprint, the snapshot of proof) and the
7
+ prebuilt client's static files at `/portal`, from the Worker's assets binding. The generated `generated/portal/main.ts` imports only this,
8
+ with the project's `blueprint.json` (made by `@orthograph/generators`) and the logbook's address from the Worker's variables. It imports no
9
+ React, no `@orthograph/portal-ui` and no HeroUI (`ortho/no-ui-in-worker`), so a project builds with no HeroUI licence.
10
+ - **The client** (`src/main.ts` and `modules/pages`): React pages drawn in the browser from `/_portal/v1/*`, with `@orthograph/portal-ui` (a
11
+ devDependency: a build input, never shipped). `nx run portal:client` builds it with vite into `dist/client` (HTML, JS, CSS), once, where the
12
+ HeroUI Pro licence is; `ortho build` copies those files into the revision (`.ortho/artefacts/<revision>/portal/`). A machine without the
13
+ licence cannot build it, and never needs to.
14
+
15
+ - `wire`: the versioned JSON the app reads, its digest, the decoder that rejects an unknown schema or a digest mismatch, and the
16
+ JSON-only adapter for what a proof rests on (kept equal to `Compare.dependsOn` by the generators' tests).
17
+ - `reader`: the logbook's open reads, injected (`Reader.http`); a logbook that does not answer is never "no verdicts".
18
+ - `snapshot`: per operation and environment, `recorded-pass`, `failed`, `stale` (with the changed declarations), `unproven` or
19
+ `unavailable`; the latest failure wins, and nothing that could not be read is ever green.
20
+ - `live`: the live view of one call, joined from a trace source and the world's journal.
21
+ - `server`: `PortalServer.handle` (data only) and `PortalServer.serve` (the Worker's route).
22
+ - `pages`: the client. Every page is `main[data-portal="page"]`, `data-ready="true"` once drawn and `data-status` its own verdict.
23
+
24
+ Tests: `nx test portal`. `snapshot/logbook.test.ts` runs the real logbook and a real OIDC issuer process (@orthograph/services) (ports 9090, 9091).
25
+ The pages are read in a browser by check 21 and the grid (`e2e/served-portal`, headless Chromium).
@@ -0,0 +1,14 @@
1
+ export { Wire } from "#modules/wire/service.js";
2
+ export { BlueprintRejected, ModelUnsupported, SCHEMA_VERSION, type Availability, type BlueprintWire, type Depends, type ModelCore, type ModelWire } from "#modules/wire/types.js";
3
+ export { Reader } from "#modules/reader/service.js";
4
+ export { LogbookUnavailable, ModelMissing, type LogbookReader, type Release, type Verdict } from "#modules/reader/types.js";
5
+ export { Snapshot } from "#modules/snapshot/service.js";
6
+ export type { ChangedDependency, PortalProof, PortalSnapshot, ProofState } from "#modules/snapshot/types.js";
7
+ export { Live } from "#modules/live/service.js";
8
+ export type { JournalRow, LiveApi, LiveCall, LiveCommit, LiveEvent, LiveLog, LiveRead, LiveSource, LiveSpan, LiveStep } from "#modules/live/types.js";
9
+ export { PortalServer } from "#modules/server/service.js";
10
+ export type { PortalAssets, PortalContext, PortalEnv, PortalLedgerBinding } from "#modules/server/types.js";
11
+ export { Telemetry } from "#modules/telemetry/service.js";
12
+ export type { CallerKind, CallsView, CountsView, MetricPoint, MetricsWindow, OperationCounts, Outcome, PortalLive, PortalTelemetry, RefusalCounts, TelemetryCall, TraceSpanView, TraceView, TraceWindow, WindowFailure, WindowLog, WindowSpan, WindowTrace } from "#modules/telemetry/types.js";
13
+ export { Monitor } from "#modules/monitor/service.js";
14
+ export type { AlertRow, IssueRow, MonitorApi, MonitorLedger, MonitorView, Seen, WindowCount } from "#modules/monitor/types.js";
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // @orthograph/portal: the read-only portal every project gets (F1, D3 #130). The wire is what the generator writes and the app
2
+ // reads; the reader is the logbook's open reads; the snapshot judges proof; the server answers `/_portal/v1/*` and
3
+ // serves the prebuilt client at `/portal` (the Worker imports `@orthograph/portal/server`, never this whole barrel: K19).
4
+ export { Wire } from "#modules/wire/service.js";
5
+ export { BlueprintRejected, ModelUnsupported, SCHEMA_VERSION } from "#modules/wire/types.js";
6
+ export { Reader } from "#modules/reader/service.js";
7
+ export { LogbookUnavailable, ModelMissing } from "#modules/reader/types.js";
8
+ export { Snapshot } from "#modules/snapshot/service.js";
9
+ export { Live } from "#modules/live/service.js";
10
+ export { PortalServer } from "#modules/server/service.js";
11
+ export { Telemetry } from "#modules/telemetry/service.js";
12
+ export { Monitor } from "#modules/monitor/service.js";
@@ -0,0 +1,5 @@
1
+ import type { JournalRow, LiveCall, LiveLog, LiveSpan } from "../types.ts";
2
+ export declare const join: (trace: string, found: {
3
+ readonly spans: ReadonlyArray<LiveSpan>;
4
+ readonly logs: ReadonlyArray<LiveLog>;
5
+ } | undefined, journal: ReadonlyArray<JournalRow>) => LiveCall;
@@ -0,0 +1,51 @@
1
+ const text = (value) => (typeof value === "string" ? value : undefined);
2
+ const count = (value) => (typeof value === "number" ? value : Number(value ?? 0) || 0);
3
+ /** A commit is a span of kind `commit` (M1.18): it shows only once it has ended, which is when a span exists. */
4
+ const commitOf = (spans) => {
5
+ const commits = spans.filter((span) => span.attrs["step.kind"] === "commit").sort((a, b) => a.start - b.start);
6
+ // A rerun after a conflict has several: the one that committed decides, else the last.
7
+ const chosen = commits.find((span) => span.attrs["commit.outcome"] === "committed") ?? commits[commits.length - 1];
8
+ return chosen === undefined ? undefined : {
9
+ outcome: text(chosen.attrs["commit.outcome"]) ?? "unknown",
10
+ writes: count(chosen.attrs["commit.writes"]),
11
+ entries: count(chosen.attrs["commit.entries"]),
12
+ };
13
+ };
14
+ /**
15
+ * Steps in the order their start signals arrived, each by its span id: running until its end signal (or its span, which
16
+ * only exists once it has ended). A step whose span is there without any signal still shows, in start order, after them.
17
+ */
18
+ const stepsOf = (spans, logs) => {
19
+ const steps = new Map();
20
+ for (const log of logs) {
21
+ const name = text(log.attrs["step"]);
22
+ const phase = text(log.attrs["phase"]);
23
+ if (name === undefined || phase === undefined)
24
+ continue;
25
+ const step = steps.get(log.spanId) ?? { name, ended: false };
26
+ if (phase === "end") {
27
+ step.ended = true;
28
+ const outcome = text(log.attrs["outcome"]);
29
+ if (outcome !== undefined)
30
+ step.outcome = outcome;
31
+ }
32
+ steps.set(log.spanId, step);
33
+ }
34
+ for (const span of [...spans].sort((a, b) => a.start - b.start)) {
35
+ const kind = text(span.attrs["step.kind"]);
36
+ if (kind === undefined || kind === "commit")
37
+ continue;
38
+ const step = steps.get(span.spanId) ?? { name: span.name, ended: true };
39
+ step.ended = true;
40
+ step.outcome ??= span.status === 2 ? "error" : "ok";
41
+ steps.set(span.spanId, step);
42
+ }
43
+ return [...steps.values()].map(({ name, ended, outcome }) => (!ended ? { name, state: "running" } : outcome === undefined ? { name, state: "ended" } : { name, state: "ended", outcome }));
44
+ };
45
+ /** An event shows only with the commit that wrote it, and only once that commit was `committed`. */
46
+ const eventsOf = (journal, commit) => commit?.outcome === "committed" ? journal.map(({ event, subject }) => ({ event, subject, commit })) : [];
47
+ export const join = (trace, found, journal) => {
48
+ const spans = found?.spans ?? [];
49
+ const commit = commitOf(spans);
50
+ return { trace, steps: stepsOf(spans, found?.logs ?? []), commit, events: eventsOf(journal, commit) };
51
+ };
@@ -0,0 +1,2 @@
1
+ import type { LiveApi } from "./types.ts";
2
+ export declare const Live: LiveApi;
@@ -0,0 +1,5 @@
1
+ import { Effect } from "effect";
2
+ import { join } from "./internal/join.js";
3
+ export const Live = {
4
+ call: (read, trace) => Effect.all({ found: read.source.find(trace), journal: read.journal(trace) }).pipe(Effect.map(({ found, journal }) => join(trace, found, journal))),
5
+ };
@@ -0,0 +1,60 @@
1
+ import type { Effect } from "effect";
2
+ /** A span as the portal reads it: only what the live view joins on. */
3
+ export interface LiveSpan {
4
+ readonly spanId: string;
5
+ readonly name: string;
6
+ readonly attrs: Readonly<Record<string, unknown>>;
7
+ /** 0 unset, 1 ok, 2 error. */
8
+ readonly status: number;
9
+ readonly start: number;
10
+ }
11
+ /** A log record as the portal reads it: a step signal is one with `step` and `phase` attributes (`handler/internal/signal.ts`). */
12
+ export interface LiveLog {
13
+ readonly spanId: string;
14
+ readonly attrs: Readonly<Record<string, unknown>>;
15
+ }
16
+ export interface LiveSource {
17
+ /** One trace by id, as it is now: no waiting, `undefined` when the source has none of it. */
18
+ readonly find: (trace: string) => Effect.Effect<{
19
+ readonly spans: ReadonlyArray<LiveSpan>;
20
+ readonly logs: ReadonlyArray<LiveLog>;
21
+ } | undefined>;
22
+ }
23
+ /** What the world's journal holds for one trace (`/_world/facts/journal?trace=`): only what the portal shows. */
24
+ export interface JournalRow {
25
+ readonly event: string;
26
+ readonly subject: string;
27
+ }
28
+ export interface LiveRead {
29
+ readonly source: LiveSource;
30
+ readonly journal: (trace: string) => Effect.Effect<ReadonlyArray<JournalRow>>;
31
+ }
32
+ /** A step of the call: `running` from its start signal, `ended` with its outcome (`ok`, `error`, `interrupted`) from its end signal or its span. */
33
+ export interface LiveStep {
34
+ readonly name: string;
35
+ readonly state: "running" | "ended";
36
+ readonly outcome?: string;
37
+ }
38
+ /** The commit, once its span has ended: counts only, never values. */
39
+ export interface LiveCommit {
40
+ readonly outcome: string;
41
+ readonly writes: number;
42
+ readonly entries: number;
43
+ }
44
+ /** An event of the call: shown only when the journal holds it and the commit that wrote it was `committed`. */
45
+ export interface LiveEvent {
46
+ readonly event: string;
47
+ readonly subject: string;
48
+ readonly commit: LiveCommit;
49
+ }
50
+ export interface LiveCall {
51
+ readonly trace: string;
52
+ /** In the order they started. A retried attempt's steps are listed again, each by its own span. */
53
+ readonly steps: ReadonlyArray<LiveStep>;
54
+ readonly commit?: LiveCommit | undefined;
55
+ readonly events: ReadonlyArray<LiveEvent>;
56
+ }
57
+ export interface LiveApi {
58
+ /** The call as it is now. Pure: reads twice, writes nothing; call it again for the next frame. */
59
+ readonly call: (read: LiveRead, trace: string) => Effect.Effect<LiveCall>;
60
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { AlertRuleWire } from "#modules/wire/types.js";
2
+ import type { TelemetryCall } from "#modules/telemetry/types.js";
3
+ import type { AlertRow } from "../types.ts";
4
+ /** `callsIn(ms)` is the calls that started within `ms` of now, or `undefined` when that window could not be read. */
5
+ export declare const evaluate: (rules: ReadonlyArray<AlertRuleWire>, callsIn: (ms: number) => ReadonlyArray<TelemetryCall> | undefined) => ReadonlyArray<AlertRow>;
@@ -0,0 +1,23 @@
1
+ const MILLIONTHS = 1000000n;
2
+ const millionths = (x) => BigInt(Math.round(x * 1_000_000));
3
+ /** `bad / calls / (1 - target) >= burn`, as `bad >= burn * (1 - target) * calls`, with no division. */
4
+ const burnsAtLeast = (bad, calls, target, burn) => calls > 0 && BigInt(bad) * MILLIONTHS * MILLIONTHS >= millionths(burn) * (MILLIONTHS - millionths(target)) * BigInt(calls);
5
+ const count = (rule, calls, ms) => {
6
+ const mine = calls.filter((c) => c.operation === rule.labels["operation"]);
7
+ return { ms, calls: mine.length, bad: mine.filter((c) => (rule.kind === "latency" ? c.durationMs > rule.thresholdMs : c.outcome === "defect")).length };
8
+ };
9
+ /** `callsIn(ms)` is the calls that started within `ms` of now, or `undefined` when that window could not be read. */
10
+ export const evaluate = (rules, callsIn) => rules.flatMap((rule) => {
11
+ const severity = rule.labels["severity"] ?? "ticket";
12
+ if (rule.alerts === undefined || rule.target === undefined)
13
+ return [{ rule: rule.id, alert: rule.kind, state: "unknown", severity }];
14
+ return rule.alerts.map((alert) => {
15
+ const read = alert.windowsMs.map((ms) => ({ ms, calls: callsIn(ms) }));
16
+ if (read.some((w) => w.calls === undefined))
17
+ return { rule: rule.id, alert: alert.name, state: "unknown", severity: alert.severity };
18
+ const windows = read.map((w) => count(rule, w.calls, w.ms));
19
+ const short = windows[windows.length - 1];
20
+ const burning = windows.every((w) => burnsAtLeast(w.bad, w.calls, rule.target, alert.burn));
21
+ return { rule: rule.id, alert: alert.name, state: burning && short.calls >= (rule.minCalls ?? 0) ? "firing" : "ok", severity: alert.severity, windows };
22
+ });
23
+ });
@@ -0,0 +1,6 @@
1
+ import { Effect } from "effect";
2
+ import type { MonitoredCall, PortalLive } from "#modules/telemetry/types.js";
3
+ import type { BlueprintWire } from "#modules/wire/types.js";
4
+ import type { IssueRow, MonitorLedger } from "../types.ts";
5
+ /** The issues the window's defects open, joined to the ones the ledger holds, written back when they changed. `calls` is `undefined` when the window could not be read. */
6
+ export declare const keep: (blueprint: BlueprintWire, live: PortalLive, calls: ReadonlyArray<MonitoredCall> | undefined, ledger: MonitorLedger | undefined) => Effect.Effect<ReadonlyArray<IssueRow>>;
@@ -0,0 +1,54 @@
1
+ // Issues: one per (operation, step, error tag), first and last seen by the operation's declaration hash, kept past the window in the ledger the portal is
2
+ // handed. The portal keeps nothing else anywhere: a different ledger is a portal that has seen nothing. A call is counted once however many times the window
3
+ // is read (`seen` holds the calls still inside the window, by id, and is pruned as they leave it: a call that left cannot come back at a later `now`).
4
+ import { Effect } from "effect";
5
+ import { Telemetry } from "#modules/telemetry/service.js";
6
+ const KEY = "monitor/issues";
7
+ const isSeen = (x) => typeof x === "object" && x !== null && typeof x.at === "number" && typeof x.hash === "string";
8
+ const isStored = (x) => {
9
+ const s = x;
10
+ return typeof s === "object" && s !== null && typeof s.operation === "string" && typeof s.step === "string" && typeof s.tag === "string" && typeof s.occurrences === "number"
11
+ && isSeen(s.firstSeen) && isSeen(s.lastSeen) && typeof s.seen === "object" && s.seen !== null;
12
+ };
13
+ /** What the ledger holds, as far as it is an issue book; anything else is an empty one (a ledger is opaque JSON the portal does not trust). */
14
+ const heldOf = (value) => typeof value === "object" && value !== null ? Object.fromEntries(Object.entries(value).filter(([, v]) => isStored(v))) : {};
15
+ const callId = (c) => Telemetry.identity(c);
16
+ const add = (held, key, call, step, tag, hash, live) => {
17
+ const issue = held[key];
18
+ const at = { at: call.start, hash };
19
+ if (issue === undefined)
20
+ return { ...held, [key]: { operation: call.operation, step, tag, occurrences: 1, firstSeen: at, lastSeen: at, seen: { [callId(call)]: call.start } } };
21
+ if (callId(call) in issue.seen)
22
+ return held;
23
+ const inWindow = Object.fromEntries(Object.entries(issue.seen).filter(([, start]) => start >= live.now - live.windowMs));
24
+ return {
25
+ ...held,
26
+ [key]: {
27
+ ...issue, occurrences: issue.occurrences + 1,
28
+ firstSeen: call.start < issue.firstSeen.at ? at : issue.firstSeen,
29
+ lastSeen: call.start > issue.lastSeen.at ? at : issue.lastSeen,
30
+ seen: { ...inWindow, [callId(call)]: call.start },
31
+ },
32
+ };
33
+ };
34
+ const rowOf = (key, issue, blueprint) => ({
35
+ key, operation: issue.operation, step: issue.step, tag: issue.tag, owner: blueprint.model.operations[issue.operation]?.file ?? "unknown",
36
+ occurrences: issue.occurrences, firstSeen: issue.firstSeen, lastSeen: issue.lastSeen, regressed: issue.firstSeen.hash !== issue.lastSeen.hash,
37
+ });
38
+ /** The issues the window's defects open, joined to the ones the ledger holds, written back when they changed. `calls` is `undefined` when the window could not be read. */
39
+ export const keep = (blueprint, live, calls, ledger) => Effect.gen(function* () {
40
+ const before = ledger === undefined ? {} : heldOf(yield* ledger.get(KEY).pipe(Effect.catchCause(() => Effect.succeed(undefined))));
41
+ let held = before;
42
+ // Oldest first, so first and last seen fall out in order and a tie is the same on every read.
43
+ const defects = (calls ?? []).filter((c) => c.outcome === "defect").sort((a, b) => a.start - b.start || (callId(a) < callId(b) ? -1 : 1));
44
+ for (const call of defects) {
45
+ const { failure } = call;
46
+ if (failure === undefined)
47
+ continue;
48
+ const hash = blueprint.model.operations[call.operation]?.hash ?? "";
49
+ held = add(held, `${call.operation}/${failure.step}/${failure.tag}`, call, failure.step, failure.tag, hash, live);
50
+ }
51
+ if (ledger !== undefined && JSON.stringify(held) !== JSON.stringify(before))
52
+ yield* ledger.put(KEY, held).pipe(Effect.catchCause(() => Effect.void));
53
+ return Object.entries(held).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([key, issue]) => rowOf(key, issue, blueprint));
54
+ });
@@ -0,0 +1,2 @@
1
+ import type { MonitorApi } from "./types.ts";
2
+ export declare const Monitor: MonitorApi;
@@ -0,0 +1,19 @@
1
+ import { Effect } from "effect";
2
+ import { Telemetry } from "#modules/telemetry/service.js";
3
+ import { evaluate } from "./internal/evaluate.js";
4
+ import { keep } from "./internal/issues.js";
5
+ const read = (blueprint, live, ledger) => Effect.gen(function* () {
6
+ const rules = (blueprint.model.alertRules ?? []).filter((r) => r.labels["environment"] === "*" || r.labels["environment"] === live.environment);
7
+ // Each distinct window an alert names is read as it is named; a source that fails leaves what it would have filled unknown, never zero.
8
+ // A window longer than the history the source can supply holds only part of it: it is not read, and an alert that needs it is `unknown`, never judged on the part.
9
+ const reach = live.historyMs ?? Number.POSITIVE_INFINITY;
10
+ // The display window is always read (the issues are its calls); an alert's window is judged only when the history reaches it.
11
+ const windows = [...new Set([live.windowMs, ...rules.flatMap((r) => (r.alerts ?? []).flatMap((a) => a.windowsMs).filter((ms) => ms <= reach))])];
12
+ const read = yield* Effect.forEach(windows, (ms) => live.source.window(ms, live.now).pipe(Effect.map((traces) => [ms, traces]), Effect.catchCause(() => Effect.succeed([ms, undefined]))));
13
+ // Each window is grouped into calls once per read; every rule and the issues read that.
14
+ const grouped = new Map(read.flatMap(([ms, traces]) => (traces === undefined ? [] : [[ms, Telemetry.calls(blueprint, traces, live.environment, { since: live.now - ms, now: live.now })]])));
15
+ const callsIn = (ms) => grouped.get(ms);
16
+ const issues = yield* keep(blueprint, live, callsIn(live.windowMs), ledger);
17
+ return { environment: live.environment, now: live.now, alerts: evaluate(rules, (ms) => (ms <= reach ? callsIn(ms) : undefined)), issues };
18
+ });
19
+ export const Monitor = { read };
@@ -0,0 +1,54 @@
1
+ import type { Effect } from "effect";
2
+ import type { BlueprintWire } from "#modules/wire/types.js";
3
+ import type { PortalLive } from "#modules/telemetry/types.js";
4
+ /** What a Worker keeps across requests, as the portal is handed it: opaque JSON by key. Where it lives is the Worker's (`portal.md` hole 7). */
5
+ export interface MonitorLedger {
6
+ readonly get: (key: string) => Effect.Effect<unknown>;
7
+ readonly put: (key: string, value: unknown) => Effect.Effect<void>;
8
+ }
9
+ /** What one window of a burn alert counted: the calls that started within `ms` of `now`, and how many were bad for its objective. */
10
+ export interface WindowCount {
11
+ readonly ms: number;
12
+ readonly calls: number;
13
+ readonly bad: number;
14
+ }
15
+ /**
16
+ * One (rule, alert) the environment evaluates. A burn alert is `firing` or `ok` with what each window counted (long, then short); a rule with no burn
17
+ * alerts (an anomaly, a verdict) is one row named for its kind, `unknown` for now: nothing judges it yet, and the portal never says ok for what it did not read.
18
+ */
19
+ export interface AlertRow {
20
+ readonly rule: string;
21
+ readonly alert: string;
22
+ readonly state: "firing" | "ok" | "unknown";
23
+ readonly severity: string;
24
+ readonly windows?: ReadonlyArray<WindowCount>;
25
+ }
26
+ /** A call's start and the operation's declaration hash in the wire in force when the portal saw it. */
27
+ export interface Seen {
28
+ readonly at: number;
29
+ readonly hash: string;
30
+ }
31
+ /** One (operation, step, error tag): a group of defects. `regressed` is the operation's declaration having changed between first and last seen. */
32
+ export interface IssueRow {
33
+ readonly key: string;
34
+ readonly operation: string;
35
+ readonly step: string;
36
+ readonly tag: string;
37
+ readonly owner: string;
38
+ /** Calls, never evaluations. */
39
+ readonly occurrences: number;
40
+ readonly firstSeen: Seen;
41
+ readonly lastSeen: Seen;
42
+ readonly regressed: boolean;
43
+ }
44
+ export interface MonitorView {
45
+ readonly environment: string;
46
+ /** The window's end: the only clock a page may show. */
47
+ readonly now: number;
48
+ readonly alerts: ReadonlyArray<AlertRow>;
49
+ readonly issues: ReadonlyArray<IssueRow>;
50
+ }
51
+ export interface MonitorApi {
52
+ /** Never fails: a source that cannot be read leaves its alerts `unknown` and the issues the ledger already holds. Reads and writes the ledger, nothing else; no clock, no network. */
53
+ readonly read: (blueprint: BlueprintWire, live: PortalLive, ledger: MonitorLedger | undefined) => Effect.Effect<MonitorView>;
54
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ import { Schema } from "effect";
2
+ export declare const VerdictRows: Schema.$Array<Schema.Struct<{
3
+ readonly seq: Schema.Number;
4
+ readonly revision: Schema.String;
5
+ readonly environment: Schema.String;
6
+ readonly operation: Schema.String;
7
+ readonly verdict: Schema.Literals<readonly ["pass", "fail"]>;
8
+ readonly repository: Schema.String;
9
+ readonly ref: Schema.String;
10
+ readonly workflow: Schema.String;
11
+ readonly event_name: Schema.NullOr<Schema.String>;
12
+ readonly sha: Schema.NullOr<Schema.String>;
13
+ }>>;
14
+ export declare const ReleaseRow: Schema.Struct<{
15
+ readonly seq: Schema.Number;
16
+ readonly version: Schema.String;
17
+ readonly revision: Schema.String;
18
+ readonly ref: Schema.String;
19
+ readonly event_name: Schema.NullOr<Schema.String>;
20
+ }>;
@@ -0,0 +1,21 @@
1
+ import { Schema } from "effect";
2
+ const Verdict = Schema.Struct({
3
+ seq: Schema.Number,
4
+ revision: Schema.String,
5
+ environment: Schema.String,
6
+ operation: Schema.String,
7
+ verdict: Schema.Literals(["pass", "fail"]),
8
+ repository: Schema.String,
9
+ ref: Schema.String,
10
+ workflow: Schema.String,
11
+ event_name: Schema.NullOr(Schema.String),
12
+ sha: Schema.NullOr(Schema.String),
13
+ });
14
+ export const VerdictRows = Schema.Array(Verdict);
15
+ export const ReleaseRow = Schema.Struct({
16
+ seq: Schema.Number,
17
+ version: Schema.String,
18
+ revision: Schema.String,
19
+ ref: Schema.String,
20
+ event_name: Schema.NullOr(Schema.String),
21
+ });
@@ -0,0 +1,2 @@
1
+ import { type ReaderApi } from "./types.ts";
2
+ export declare const Reader: ReaderApi;