@loop-engine/observability 0.1.0 → 0.1.2

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/LICENSE CHANGED
@@ -1,21 +1,203 @@
1
- MIT License
2
-
3
- Copyright (c) Loop Engine Contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ Copyright 2026 Better Data, Inc.
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright [yyyy] [name of copyright owner]
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @loop-engine/observability
2
+
3
+ [![npm](https://img.shields.io/npm/v/@loop-engine/observability.svg)](https://www.npmjs.com/package/@loop-engine/observability)
4
+ [![Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
5
+ [![Loop Engine](https://img.shields.io/badge/loopengine.io-docs-blue)](https://loopengine.io/docs)
6
+
7
+ Loop timeline reconstruction, replay validation, and observability helpers for Loop Engine.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @loop-engine/observability
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import { buildTimeline, replayLoop } from "@loop-engine/observability";
19
+
20
+ const timeline = buildTimeline(instance, history);
21
+ const replay = replayLoop(definition, history);
22
+
23
+ console.log({
24
+ aggregateId: timeline.aggregateId,
25
+ durationMs: timeline.durationMs,
26
+ transitions: timeline.transitions.length,
27
+ replayValid: replay.valid
28
+ });
29
+ ```
30
+
31
+ ## Documentation link
32
+
33
+ https://loopengine.io/docs/packages/observability
34
+
35
+ ## License
36
+
37
+ Apache-2.0 © Better Data, Inc.
package/dist/index.cjs ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ buildTimeline: () => buildTimeline,
24
+ computeMetrics: () => computeMetrics,
25
+ getStateResidency: () => getStateResidency,
26
+ replayLoop: () => replayLoop
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/metrics.ts
31
+ function percentile(values, p) {
32
+ if (values.length === 0) return 0;
33
+ const sorted = [...values].sort((a, b) => a - b);
34
+ const idx = Math.min(sorted.length - 1, Math.floor(p / 100 * sorted.length));
35
+ return sorted[idx] ?? 0;
36
+ }
37
+ function computeMetrics(instances, history, period) {
38
+ const first = instances[0];
39
+ const loop = first?.loopId ?? "unknown.loop";
40
+ const total = instances.length;
41
+ const open = instances.filter((instance) => instance.status === "active").length;
42
+ const closed = instances.filter((instance) => instance.status === "completed").length;
43
+ const error = instances.filter((instance) => instance.status === "failed").length;
44
+ const durations = instances.map(
45
+ (instance) => instance.completedAt ? Date.parse(instance.completedAt) - Date.parse(instance.startedAt) : 0
46
+ ).filter((duration) => duration > 0);
47
+ const avg = durations.length ? durations.reduce((a, b) => a + b, 0) / durations.length : 0;
48
+ const aiTransitions = history.filter((record) => record.actor.type === "ai-agent").length;
49
+ const humanTransitions = history.filter((record) => record.actor.type === "human").length;
50
+ const totalTransitions = history.length;
51
+ const byAggregate = /* @__PURE__ */ new Map();
52
+ for (const record of history) {
53
+ byAggregate.set(record.aggregateId, (byAggregate.get(record.aggregateId) ?? 0) + 1);
54
+ }
55
+ return {
56
+ loopId: loop,
57
+ period,
58
+ totalInstances: total,
59
+ openInstances: open,
60
+ closedInstances: closed,
61
+ errorInstances: error,
62
+ avgDurationMs: avg,
63
+ medianDurationMs: percentile(durations, 50),
64
+ p95DurationMs: percentile(durations, 95),
65
+ completionRate: closed + error > 0 ? closed / (closed + error) : 0,
66
+ guardFailureRate: 0,
67
+ aiActorRate: totalTransitions > 0 ? aiTransitions / totalTransitions : 0,
68
+ humanActorRate: totalTransitions > 0 ? humanTransitions / totalTransitions : 0,
69
+ avgTransitionCount: byAggregate.size > 0 ? [...byAggregate.values()].reduce((a, b) => a + b, 0) / byAggregate.size : 0
70
+ };
71
+ }
72
+
73
+ // src/timeline.ts
74
+ function buildTimeline(instance, history) {
75
+ const end = instance.completedAt ?? (/* @__PURE__ */ new Date()).toISOString();
76
+ return {
77
+ aggregateId: instance.aggregateId,
78
+ loopId: instance.loopId,
79
+ instance,
80
+ transitions: [...history].sort(
81
+ (left, right) => Date.parse(left.occurredAt) - Date.parse(right.occurredAt)
82
+ ),
83
+ durationMs: Math.max(0, Date.parse(end) - Date.parse(instance.startedAt)),
84
+ isComplete: instance.status === "completed"
85
+ };
86
+ }
87
+ function getStateResidency(timeline) {
88
+ const out = [];
89
+ let currentState = timeline.instance.currentState;
90
+ let enteredAt = timeline.instance.startedAt;
91
+ for (const t of timeline.transitions) {
92
+ out.push({
93
+ stateId: currentState,
94
+ enteredAt,
95
+ exitedAt: t.occurredAt,
96
+ durationMs: Math.max(0, Date.parse(t.occurredAt) - Date.parse(enteredAt))
97
+ });
98
+ currentState = t.toState;
99
+ enteredAt = t.occurredAt;
100
+ }
101
+ out.push({
102
+ stateId: currentState,
103
+ enteredAt,
104
+ ...timeline.instance.completedAt ? { exitedAt: timeline.instance.completedAt } : {},
105
+ ...timeline.instance.completedAt ? { durationMs: Math.max(0, Date.parse(timeline.instance.completedAt) - Date.parse(enteredAt)) } : {}
106
+ });
107
+ return out;
108
+ }
109
+
110
+ // src/replay.ts
111
+ function replayLoop(definition, history) {
112
+ const errors = [];
113
+ let state = definition.initialState;
114
+ for (const record of history) {
115
+ const match = definition.transitions.find(
116
+ (transition) => transition.transitionId === record.transitionId && transition.from === state && transition.to === record.toState
117
+ );
118
+ if (!match) {
119
+ errors.push(`Invalid transition ${record.transitionId} from ${state} to ${record.toState}`);
120
+ continue;
121
+ }
122
+ state = record.toState;
123
+ }
124
+ return { valid: errors.length === 0, errors };
125
+ }
126
+ // Annotate the CommonJS export names for ESM import in node:
127
+ 0 && (module.exports = {
128
+ buildTimeline,
129
+ computeMetrics,
130
+ getStateResidency,
131
+ replayLoop
132
+ });
133
+ // @license Apache-2.0
134
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/metrics.ts","../src/timeline.ts","../src/replay.ts"],"sourcesContent":["// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nexport * from \"./metrics\";\nexport * from \"./timeline\";\nexport * from \"./replay\";\n","// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { LoopId } from \"@loop-engine/core\";\nimport type { RuntimeLoopInstance, RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport interface LoopMetrics {\n loopId: LoopId;\n period: { from: string; to: string };\n totalInstances: number;\n openInstances: number;\n closedInstances: number;\n errorInstances: number;\n avgDurationMs: number;\n medianDurationMs: number;\n p95DurationMs: number;\n completionRate: number;\n guardFailureRate: number;\n aiActorRate: number;\n humanActorRate: number;\n avgTransitionCount: number;\n}\n\nfunction percentile(values: number[], p: number): number {\n if (values.length === 0) return 0;\n const sorted = [...values].sort((a, b) => a - b);\n const idx = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length));\n return sorted[idx] ?? 0;\n}\n\nexport function computeMetrics(\n instances: RuntimeLoopInstance[],\n history: RuntimeTransitionRecord[],\n period: { from: string; to: string }\n): LoopMetrics {\n const first = instances[0];\n const loop = (first?.loopId ?? \"unknown.loop\") as LoopId;\n const total = instances.length;\n const open = instances.filter((instance) => instance.status === \"active\").length;\n const closed = instances.filter((instance) => instance.status === \"completed\").length;\n const error = instances.filter((instance) => instance.status === \"failed\").length;\n\n const durations = instances\n .map((instance) =>\n instance.completedAt ? Date.parse(instance.completedAt) - Date.parse(instance.startedAt) : 0\n )\n .filter((duration) => duration > 0);\n const avg = durations.length ? durations.reduce((a, b) => a + b, 0) / durations.length : 0;\n const aiTransitions = history.filter((record) => record.actor.type === \"ai-agent\").length;\n const humanTransitions = history.filter((record) => record.actor.type === \"human\").length;\n const totalTransitions = history.length;\n\n const byAggregate = new Map<string, number>();\n for (const record of history) {\n byAggregate.set(record.aggregateId, (byAggregate.get(record.aggregateId) ?? 0) + 1);\n }\n\n return {\n loopId: loop,\n period,\n totalInstances: total,\n openInstances: open,\n closedInstances: closed,\n errorInstances: error,\n avgDurationMs: avg,\n medianDurationMs: percentile(durations, 50),\n p95DurationMs: percentile(durations, 95),\n completionRate: closed + error > 0 ? closed / (closed + error) : 0,\n guardFailureRate: 0,\n aiActorRate: totalTransitions > 0 ? aiTransitions / totalTransitions : 0,\n humanActorRate: totalTransitions > 0 ? humanTransitions / totalTransitions : 0,\n avgTransitionCount:\n byAggregate.size > 0\n ? [...byAggregate.values()].reduce((a, b) => a + b, 0) / byAggregate.size\n : 0\n };\n}\n","// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { AggregateId, StateId } from \"@loop-engine/core\";\nimport type { RuntimeLoopInstance, RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport interface LoopTimeline {\n aggregateId: AggregateId;\n loopId: RuntimeLoopInstance[\"loopId\"];\n instance: RuntimeLoopInstance;\n transitions: RuntimeTransitionRecord[];\n durationMs: number;\n isComplete: boolean;\n}\n\nexport interface StateResidency {\n stateId: StateId;\n enteredAt: string;\n exitedAt?: string;\n durationMs?: number;\n}\n\nexport function buildTimeline(\n instance: RuntimeLoopInstance,\n history: RuntimeTransitionRecord[]\n): LoopTimeline {\n const end = instance.completedAt ?? new Date().toISOString();\n return {\n aggregateId: instance.aggregateId,\n loopId: instance.loopId,\n instance,\n transitions: [...history].sort(\n (left, right) => Date.parse(left.occurredAt) - Date.parse(right.occurredAt)\n ),\n durationMs: Math.max(0, Date.parse(end) - Date.parse(instance.startedAt)),\n isComplete: instance.status === \"completed\"\n };\n}\n\nexport function getStateResidency(timeline: LoopTimeline): StateResidency[] {\n const out: StateResidency[] = [];\n let currentState = timeline.instance.currentState;\n let enteredAt = timeline.instance.startedAt;\n for (const t of timeline.transitions) {\n out.push({\n stateId: currentState,\n enteredAt,\n exitedAt: t.occurredAt,\n durationMs: Math.max(0, Date.parse(t.occurredAt) - Date.parse(enteredAt))\n });\n currentState = t.toState;\n enteredAt = t.occurredAt;\n }\n out.push({\n stateId: currentState,\n enteredAt,\n ...(timeline.instance.completedAt ? { exitedAt: timeline.instance.completedAt } : {}),\n ...(timeline.instance.completedAt\n ? { durationMs: Math.max(0, Date.parse(timeline.instance.completedAt) - Date.parse(enteredAt)) }\n : {})\n });\n return out;\n}\n","// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { LoopDefinition } from \"@loop-engine/core\";\nimport type { RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport function replayLoop(\n definition: LoopDefinition,\n history: RuntimeTransitionRecord[]\n): { valid: boolean; errors: string[] } {\n const errors: string[] = [];\n let state = definition.initialState;\n for (const record of history) {\n const match = definition.transitions.find(\n (transition) =>\n transition.transitionId === record.transitionId &&\n transition.from === state &&\n transition.to === record.toState\n );\n if (!match) {\n errors.push(`Invalid transition ${record.transitionId} from ${state} to ${record.toState}`);\n continue;\n }\n state = record.toState;\n }\n return { valid: errors.length === 0, errors };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACsBA,SAAS,WAAW,QAAkB,GAAmB;AACvD,MAAI,OAAO,WAAW,EAAG,QAAO;AAChC,QAAM,SAAS,CAAC,GAAG,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAC/C,QAAM,MAAM,KAAK,IAAI,OAAO,SAAS,GAAG,KAAK,MAAO,IAAI,MAAO,OAAO,MAAM,CAAC;AAC7E,SAAO,OAAO,GAAG,KAAK;AACxB;AAEO,SAAS,eACd,WACA,SACA,QACa;AACb,QAAM,QAAQ,UAAU,CAAC;AACzB,QAAM,OAAQ,OAAO,UAAU;AAC/B,QAAM,QAAQ,UAAU;AACxB,QAAM,OAAO,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,QAAQ,EAAE;AAC1E,QAAM,SAAS,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,WAAW,EAAE;AAC/E,QAAM,QAAQ,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,QAAQ,EAAE;AAE3E,QAAM,YAAY,UACf;AAAA,IAAI,CAAC,aACJ,SAAS,cAAc,KAAK,MAAM,SAAS,WA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OAAO,OAAO,OAAO,EAAE;AAC1F;AAAA,IACF;AACA,YAAQ,OAAO;AAAA,EACjB;AACA,SAAO,EAAE,OAAO,OAAO,WAAW,GAAG,OAAO;AAC9C;","names":[]}
@@ -0,0 +1,50 @@
1
+ import { LoopId, AggregateId, StateId, LoopDefinition } from '@loop-engine/core';
2
+ import { RuntimeLoopInstance, RuntimeTransitionRecord } from '@loop-engine/runtime';
3
+
4
+ interface LoopMetrics {
5
+ loopId: LoopId;
6
+ period: {
7
+ from: string;
8
+ to: string;
9
+ };
10
+ totalInstances: number;
11
+ openInstances: number;
12
+ closedInstances: number;
13
+ errorInstances: number;
14
+ avgDurationMs: number;
15
+ medianDurationMs: number;
16
+ p95DurationMs: number;
17
+ completionRate: number;
18
+ guardFailureRate: number;
19
+ aiActorRate: number;
20
+ humanActorRate: number;
21
+ avgTransitionCount: number;
22
+ }
23
+ declare function computeMetrics(instances: RuntimeLoopInstance[], history: RuntimeTransitionRecord[], period: {
24
+ from: string;
25
+ to: string;
26
+ }): LoopMetrics;
27
+
28
+ interface LoopTimeline {
29
+ aggregateId: AggregateId;
30
+ loopId: RuntimeLoopInstance["loopId"];
31
+ instance: RuntimeLoopInstance;
32
+ transitions: RuntimeTransitionRecord[];
33
+ durationMs: number;
34
+ isComplete: boolean;
35
+ }
36
+ interface StateResidency {
37
+ stateId: StateId;
38
+ enteredAt: string;
39
+ exitedAt?: string;
40
+ durationMs?: number;
41
+ }
42
+ declare function buildTimeline(instance: RuntimeLoopInstance, history: RuntimeTransitionRecord[]): LoopTimeline;
43
+ declare function getStateResidency(timeline: LoopTimeline): StateResidency[];
44
+
45
+ declare function replayLoop(definition: LoopDefinition, history: RuntimeTransitionRecord[]): {
46
+ valid: boolean;
47
+ errors: string[];
48
+ };
49
+
50
+ export { type LoopMetrics, type LoopTimeline, type StateResidency, buildTimeline, computeMetrics, getStateResidency, replayLoop };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,50 @@
1
- export * from "./metrics";
2
- export * from "./timeline";
3
- export * from "./replay";
4
- //# sourceMappingURL=index.d.ts.map
1
+ import { LoopId, AggregateId, StateId, LoopDefinition } from '@loop-engine/core';
2
+ import { RuntimeLoopInstance, RuntimeTransitionRecord } from '@loop-engine/runtime';
3
+
4
+ interface LoopMetrics {
5
+ loopId: LoopId;
6
+ period: {
7
+ from: string;
8
+ to: string;
9
+ };
10
+ totalInstances: number;
11
+ openInstances: number;
12
+ closedInstances: number;
13
+ errorInstances: number;
14
+ avgDurationMs: number;
15
+ medianDurationMs: number;
16
+ p95DurationMs: number;
17
+ completionRate: number;
18
+ guardFailureRate: number;
19
+ aiActorRate: number;
20
+ humanActorRate: number;
21
+ avgTransitionCount: number;
22
+ }
23
+ declare function computeMetrics(instances: RuntimeLoopInstance[], history: RuntimeTransitionRecord[], period: {
24
+ from: string;
25
+ to: string;
26
+ }): LoopMetrics;
27
+
28
+ interface LoopTimeline {
29
+ aggregateId: AggregateId;
30
+ loopId: RuntimeLoopInstance["loopId"];
31
+ instance: RuntimeLoopInstance;
32
+ transitions: RuntimeTransitionRecord[];
33
+ durationMs: number;
34
+ isComplete: boolean;
35
+ }
36
+ interface StateResidency {
37
+ stateId: StateId;
38
+ enteredAt: string;
39
+ exitedAt?: string;
40
+ durationMs?: number;
41
+ }
42
+ declare function buildTimeline(instance: RuntimeLoopInstance, history: RuntimeTransitionRecord[]): LoopTimeline;
43
+ declare function getStateResidency(timeline: LoopTimeline): StateResidency[];
44
+
45
+ declare function replayLoop(definition: LoopDefinition, history: RuntimeTransitionRecord[]): {
46
+ valid: boolean;
47
+ errors: string[];
48
+ };
49
+
50
+ export { type LoopMetrics, type LoopTimeline, type StateResidency, buildTimeline, computeMetrics, getStateResidency, replayLoop };
package/dist/index.js CHANGED
@@ -1,6 +1,104 @@
1
- // @license MIT
2
- // SPDX-License-Identifier: MIT
3
- export * from "./metrics";
4
- export * from "./timeline";
5
- export * from "./replay";
1
+ // src/metrics.ts
2
+ function percentile(values, p) {
3
+ if (values.length === 0) return 0;
4
+ const sorted = [...values].sort((a, b) => a - b);
5
+ const idx = Math.min(sorted.length - 1, Math.floor(p / 100 * sorted.length));
6
+ return sorted[idx] ?? 0;
7
+ }
8
+ function computeMetrics(instances, history, period) {
9
+ const first = instances[0];
10
+ const loop = first?.loopId ?? "unknown.loop";
11
+ const total = instances.length;
12
+ const open = instances.filter((instance) => instance.status === "active").length;
13
+ const closed = instances.filter((instance) => instance.status === "completed").length;
14
+ const error = instances.filter((instance) => instance.status === "failed").length;
15
+ const durations = instances.map(
16
+ (instance) => instance.completedAt ? Date.parse(instance.completedAt) - Date.parse(instance.startedAt) : 0
17
+ ).filter((duration) => duration > 0);
18
+ const avg = durations.length ? durations.reduce((a, b) => a + b, 0) / durations.length : 0;
19
+ const aiTransitions = history.filter((record) => record.actor.type === "ai-agent").length;
20
+ const humanTransitions = history.filter((record) => record.actor.type === "human").length;
21
+ const totalTransitions = history.length;
22
+ const byAggregate = /* @__PURE__ */ new Map();
23
+ for (const record of history) {
24
+ byAggregate.set(record.aggregateId, (byAggregate.get(record.aggregateId) ?? 0) + 1);
25
+ }
26
+ return {
27
+ loopId: loop,
28
+ period,
29
+ totalInstances: total,
30
+ openInstances: open,
31
+ closedInstances: closed,
32
+ errorInstances: error,
33
+ avgDurationMs: avg,
34
+ medianDurationMs: percentile(durations, 50),
35
+ p95DurationMs: percentile(durations, 95),
36
+ completionRate: closed + error > 0 ? closed / (closed + error) : 0,
37
+ guardFailureRate: 0,
38
+ aiActorRate: totalTransitions > 0 ? aiTransitions / totalTransitions : 0,
39
+ humanActorRate: totalTransitions > 0 ? humanTransitions / totalTransitions : 0,
40
+ avgTransitionCount: byAggregate.size > 0 ? [...byAggregate.values()].reduce((a, b) => a + b, 0) / byAggregate.size : 0
41
+ };
42
+ }
43
+
44
+ // src/timeline.ts
45
+ function buildTimeline(instance, history) {
46
+ const end = instance.completedAt ?? (/* @__PURE__ */ new Date()).toISOString();
47
+ return {
48
+ aggregateId: instance.aggregateId,
49
+ loopId: instance.loopId,
50
+ instance,
51
+ transitions: [...history].sort(
52
+ (left, right) => Date.parse(left.occurredAt) - Date.parse(right.occurredAt)
53
+ ),
54
+ durationMs: Math.max(0, Date.parse(end) - Date.parse(instance.startedAt)),
55
+ isComplete: instance.status === "completed"
56
+ };
57
+ }
58
+ function getStateResidency(timeline) {
59
+ const out = [];
60
+ let currentState = timeline.instance.currentState;
61
+ let enteredAt = timeline.instance.startedAt;
62
+ for (const t of timeline.transitions) {
63
+ out.push({
64
+ stateId: currentState,
65
+ enteredAt,
66
+ exitedAt: t.occurredAt,
67
+ durationMs: Math.max(0, Date.parse(t.occurredAt) - Date.parse(enteredAt))
68
+ });
69
+ currentState = t.toState;
70
+ enteredAt = t.occurredAt;
71
+ }
72
+ out.push({
73
+ stateId: currentState,
74
+ enteredAt,
75
+ ...timeline.instance.completedAt ? { exitedAt: timeline.instance.completedAt } : {},
76
+ ...timeline.instance.completedAt ? { durationMs: Math.max(0, Date.parse(timeline.instance.completedAt) - Date.parse(enteredAt)) } : {}
77
+ });
78
+ return out;
79
+ }
80
+
81
+ // src/replay.ts
82
+ function replayLoop(definition, history) {
83
+ const errors = [];
84
+ let state = definition.initialState;
85
+ for (const record of history) {
86
+ const match = definition.transitions.find(
87
+ (transition) => transition.transitionId === record.transitionId && transition.from === state && transition.to === record.toState
88
+ );
89
+ if (!match) {
90
+ errors.push(`Invalid transition ${record.transitionId} from ${state} to ${record.toState}`);
91
+ continue;
92
+ }
93
+ state = record.toState;
94
+ }
95
+ return { valid: errors.length === 0, errors };
96
+ }
97
+ export {
98
+ buildTimeline,
99
+ computeMetrics,
100
+ getStateResidency,
101
+ replayLoop
102
+ };
103
+ // @license Apache-2.0
6
104
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,+BAA+B;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"sources":["../src/metrics.ts","../src/timeline.ts","../src/replay.ts"],"sourcesContent":["// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { LoopId } from \"@loop-engine/core\";\nimport type { RuntimeLoopInstance, RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport interface LoopMetrics {\n loopId: LoopId;\n period: { from: string; to: string };\n totalInstances: number;\n openInstances: number;\n closedInstances: number;\n errorInstances: number;\n avgDurationMs: number;\n medianDurationMs: number;\n p95DurationMs: number;\n completionRate: number;\n guardFailureRate: number;\n aiActorRate: number;\n humanActorRate: number;\n avgTransitionCount: number;\n}\n\nfunction percentile(values: number[], p: number): number {\n if (values.length === 0) return 0;\n const sorted = [...values].sort((a, b) => a - b);\n const idx = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length));\n return sorted[idx] ?? 0;\n}\n\nexport function computeMetrics(\n instances: RuntimeLoopInstance[],\n history: RuntimeTransitionRecord[],\n period: { from: string; to: string }\n): LoopMetrics {\n const first = instances[0];\n const loop = (first?.loopId ?? \"unknown.loop\") as LoopId;\n const total = instances.length;\n const open = instances.filter((instance) => instance.status === \"active\").length;\n const closed = instances.filter((instance) => instance.status === \"completed\").length;\n const error = instances.filter((instance) => instance.status === \"failed\").length;\n\n const durations = instances\n .map((instance) =>\n instance.completedAt ? Date.parse(instance.completedAt) - Date.parse(instance.startedAt) : 0\n )\n .filter((duration) => duration > 0);\n const avg = durations.length ? durations.reduce((a, b) => a + b, 0) / durations.length : 0;\n const aiTransitions = history.filter((record) => record.actor.type === \"ai-agent\").length;\n const humanTransitions = history.filter((record) => record.actor.type === \"human\").length;\n const totalTransitions = history.length;\n\n const byAggregate = new Map<string, number>();\n for (const record of history) {\n byAggregate.set(record.aggregateId, (byAggregate.get(record.aggregateId) ?? 0) + 1);\n }\n\n return {\n loopId: loop,\n period,\n totalInstances: total,\n openInstances: open,\n closedInstances: closed,\n errorInstances: error,\n avgDurationMs: avg,\n medianDurationMs: percentile(durations, 50),\n p95DurationMs: percentile(durations, 95),\n completionRate: closed + error > 0 ? closed / (closed + error) : 0,\n guardFailureRate: 0,\n aiActorRate: totalTransitions > 0 ? aiTransitions / totalTransitions : 0,\n humanActorRate: totalTransitions > 0 ? humanTransitions / totalTransitions : 0,\n avgTransitionCount:\n byAggregate.size > 0\n ? [...byAggregate.values()].reduce((a, b) => a + b, 0) / byAggregate.size\n : 0\n };\n}\n","// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { AggregateId, StateId } from \"@loop-engine/core\";\nimport type { RuntimeLoopInstance, RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport interface LoopTimeline {\n aggregateId: AggregateId;\n loopId: RuntimeLoopInstance[\"loopId\"];\n instance: RuntimeLoopInstance;\n transitions: RuntimeTransitionRecord[];\n durationMs: number;\n isComplete: boolean;\n}\n\nexport interface StateResidency {\n stateId: StateId;\n enteredAt: string;\n exitedAt?: string;\n durationMs?: number;\n}\n\nexport function buildTimeline(\n instance: RuntimeLoopInstance,\n history: RuntimeTransitionRecord[]\n): LoopTimeline {\n const end = instance.completedAt ?? new Date().toISOString();\n return {\n aggregateId: instance.aggregateId,\n loopId: instance.loopId,\n instance,\n transitions: [...history].sort(\n (left, right) => Date.parse(left.occurredAt) - Date.parse(right.occurredAt)\n ),\n durationMs: Math.max(0, Date.parse(end) - Date.parse(instance.startedAt)),\n isComplete: instance.status === \"completed\"\n };\n}\n\nexport function getStateResidency(timeline: LoopTimeline): StateResidency[] {\n const out: StateResidency[] = [];\n let currentState = timeline.instance.currentState;\n let enteredAt = timeline.instance.startedAt;\n for (const t of timeline.transitions) {\n out.push({\n stateId: currentState,\n enteredAt,\n exitedAt: t.occurredAt,\n durationMs: Math.max(0, Date.parse(t.occurredAt) - Date.parse(enteredAt))\n });\n currentState = t.toState;\n enteredAt = t.occurredAt;\n }\n out.push({\n stateId: currentState,\n enteredAt,\n ...(timeline.instance.completedAt ? { exitedAt: timeline.instance.completedAt } : {}),\n ...(timeline.instance.completedAt\n ? { durationMs: Math.max(0, Date.parse(timeline.instance.completedAt) - Date.parse(enteredAt)) }\n : {})\n });\n return out;\n}\n","// @license Apache-2.0\n// SPDX-License-Identifier: Apache-2.0\nimport type { LoopDefinition } from \"@loop-engine/core\";\nimport type { RuntimeTransitionRecord } from \"@loop-engine/runtime\";\n\nexport function replayLoop(\n definition: LoopDefinition,\n history: RuntimeTransitionRecord[]\n): { valid: boolean; errors: string[] } {\n const errors: string[] = [];\n let state = definition.initialState;\n for (const record of history) {\n const match = definition.transitions.find(\n (transition) =>\n transition.transitionId === record.transitionId &&\n transition.from === state &&\n transition.to === record.toState\n );\n if (!match) {\n errors.push(`Invalid transition ${record.transitionId} from ${state} to ${record.toState}`);\n continue;\n }\n state = record.toState;\n }\n return { valid: errors.length === 0, errors };\n}\n"],"mappings":";AAsBA,SAAS,WAAW,QAAkB,GAAmB;AACvD,MAAI,OAAO,WAAW,EAAG,QAAO;AAChC,QAAM,SAAS,CAAC,GAAG,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAC/C,QAAM,MAAM,KAAK,IAAI,OAAO,SAAS,GAAG,KAAK,MAAO,IAAI,MAAO,OAAO,MAAM,CAAC;AAC7E,SAAO,OAAO,GAAG,KAAK;AACxB;AAEO,SAAS,eACd,WACA,SACA,QACa;AACb,QAAM,QAAQ,UAAU,CAAC;AACzB,QAAM,OAAQ,OAAO,UAAU;AAC/B,QAAM,QAAQ,UAAU;AACxB,QAAM,OAAO,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,QAAQ,EAAE;AAC1E,QAAM,SAAS,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,WAAW,EAAE;AAC/E,QAAM,QAAQ,UAAU,OAAO,CAAC,aAAa,SAAS,WAAW,QAAQ,EAAE;AAE3E,QAAM,YAAY,UACf;AAAA,IAAI,CAAC,aACJ,SAAS,cAAc,K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,YAAY,SAAS,KAAK,OAAO,OAAO,OAAO,EAAE;AAC1F;AAAA,IACF;AACA,YAAQ,OAAO;AAAA,EACjB;AACA,SAAO,EAAE,OAAO,OAAO,WAAW,GAAG,OAAO;AAC9C;","names":[]}
package/package.json CHANGED
@@ -1,29 +1,51 @@
1
1
  {
2
2
  "name": "@loop-engine/observability",
3
- "version": "0.1.0",
4
- "license": "MIT",
3
+ "version": "0.1.2",
4
+ "description": "Metrics, timelines, and replay utilities for Loop Engine execution traces",
5
+ "license": "Apache-2.0",
6
+ "keywords": [
7
+ "loop-engine",
8
+ "workflow",
9
+ "governance",
10
+ "state-machine",
11
+ "audit-trail",
12
+ "actors",
13
+ "guards",
14
+ "enterprise",
15
+ "supply-chain",
16
+ "decision-governance",
17
+ "observability",
18
+ "timeline",
19
+ "replay",
20
+ "devtools"
21
+ ],
5
22
  "type": "module",
6
- "main": "./dist/index.js",
23
+ "main": "./dist/index.cjs",
7
24
  "module": "./dist/index.js",
8
25
  "types": "./dist/index.d.ts",
9
26
  "exports": {
10
27
  ".": {
11
28
  "import": "./dist/index.js",
12
- "require": "./dist/index.js",
29
+ "require": "./dist/index.cjs",
13
30
  "types": "./dist/index.d.ts"
14
31
  }
15
32
  },
33
+ "scripts": {
34
+ "build": "tsup",
35
+ "typecheck": "tsc -p tsconfig.json --noEmit",
36
+ "test": "vitest run"
37
+ },
16
38
  "dependencies": {
17
- "@loop-engine/core": "0.1.0"
39
+ "@loop-engine/core": "workspace:*",
40
+ "@loop-engine/runtime": "workspace:*"
18
41
  },
42
+ "devDependencies": {},
19
43
  "files": [
20
44
  "dist/",
21
45
  "README.md",
22
46
  "LICENSE"
23
47
  ],
24
- "scripts": {
25
- "build": "tsc -p tsconfig.json",
26
- "typecheck": "tsc -p tsconfig.json --noEmit",
27
- "test": "vitest run"
48
+ "engines": {
49
+ "node": ">=18.0.0"
28
50
  }
29
- }
51
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
package/dist/metrics.d.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { LoopId, LoopInstance, TransitionRecord } from "@loop-engine/core";
2
- export interface LoopMetrics {
3
- loopId: LoopId;
4
- period: {
5
- from: string;
6
- to: string;
7
- };
8
- totalInstances: number;
9
- openInstances: number;
10
- closedInstances: number;
11
- errorInstances: number;
12
- avgDurationMs: number;
13
- medianDurationMs: number;
14
- p95DurationMs: number;
15
- completionRate: number;
16
- guardFailureRate: number;
17
- aiActorRate: number;
18
- humanActorRate: number;
19
- avgTransitionCount: number;
20
- }
21
- export declare function computeMetrics(instances: LoopInstance[], history: TransitionRecord[], period: {
22
- from: string;
23
- to: string;
24
- }): LoopMetrics;
25
- //# sourceMappingURL=metrics.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEhF,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IACrC,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AASD,wBAAgB,cAAc,CAC5B,SAAS,EAAE,YAAY,EAAE,EACzB,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACnC,WAAW,CAwCb"}
package/dist/metrics.js DELETED
@@ -1,45 +0,0 @@
1
- function percentile(values, p) {
2
- if (values.length === 0)
3
- return 0;
4
- const sorted = [...values].sort((a, b) => a - b);
5
- const idx = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length));
6
- return sorted[idx] ?? 0;
7
- }
8
- export function computeMetrics(instances, history, period) {
9
- const first = instances[0];
10
- const loop = (first?.loopId ?? "unknown.loop");
11
- const total = instances.length;
12
- const open = instances.filter((i) => i.status === "OPEN" || i.status === "IN_PROGRESS").length;
13
- const closed = instances.filter((i) => i.status === "CLOSED").length;
14
- const error = instances.filter((i) => i.status === "ERROR").length;
15
- const durations = instances
16
- .map((i) => (i.closedAt ? Date.parse(i.closedAt) - Date.parse(i.startedAt) : 0))
17
- .filter((d) => d > 0);
18
- const avg = durations.length ? durations.reduce((a, b) => a + b, 0) / durations.length : 0;
19
- const aiTransitions = history.filter((r) => r.actor.type === "ai-agent").length;
20
- const humanTransitions = history.filter((r) => r.actor.type === "human").length;
21
- const totalTransitions = history.length;
22
- const byAggregate = new Map();
23
- for (const r of history) {
24
- byAggregate.set(r.aggregateId, (byAggregate.get(r.aggregateId) ?? 0) + 1);
25
- }
26
- return {
27
- loopId: loop,
28
- period,
29
- totalInstances: total,
30
- openInstances: open,
31
- closedInstances: closed,
32
- errorInstances: error,
33
- avgDurationMs: avg,
34
- medianDurationMs: percentile(durations, 50),
35
- p95DurationMs: percentile(durations, 95),
36
- completionRate: closed + error > 0 ? closed / (closed + error) : 0,
37
- guardFailureRate: 0,
38
- aiActorRate: totalTransitions > 0 ? aiTransitions / totalTransitions : 0,
39
- humanActorRate: totalTransitions > 0 ? humanTransitions / totalTransitions : 0,
40
- avgTransitionCount: byAggregate.size > 0
41
- ? [...byAggregate.values()].reduce((a, b) => a + b, 0) / byAggregate.size
42
- : 0
43
- };
44
- }
45
- //# sourceMappingURL=metrics.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"metrics.js","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":"AAqBA,SAAS,UAAU,CAAC,MAAgB,EAAE,CAAS;IAC7C,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACjD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/E,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,cAAc,CAC5B,SAAyB,EACzB,OAA2B,EAC3B,MAAoC;IAEpC,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;IAC3B,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,MAAM,IAAI,cAAc,CAAW,CAAC;IACzD,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC;IAC/B,MAAM,IAAI,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,IAAI,CAAC,CAAC,MAAM,KAAK,aAAa,CAAC,CAAC,MAAM,CAAC;IAC/F,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MA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,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5E,CAAC;IAED,OAAO;QACL,MAAM,EAAE,IAAI;QACZ,MAAM;QACN,cAAc,EAAE,KAAK;QACrB,aAAa,EAAE,IAAI;QACnB,eAAe,EAAE,MAAM;QACvB,cAAc,EAAE,KAAK;QACrB,aAAa,EAAE,GAAG;QAClB,gBAAgB,EAAE,UAAU,CAAC,SAAS,EAAE,EAAE,CAAC;QAC3C,aAAa,EAAE,UAAU,CAAC,SAAS,EAAE,EAAE,CAAC;QACxC,cAAc,EAAE,MAAM,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAClE,gBAAgB,EAAE,CAAC;QACnB,WAAW,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;QACxE,cAAc,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;QAC9E,kBAAkB,EAChB,WAAW,CAAC,IAAI,GAAG,CAAC;YAClB,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,WAAW,CAAC,IAAI;YACzE,CAAC,CAAC,CAAC;KACR,CAAC;AACJ,CAAC"}
package/dist/replay.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import type { LoopDefinition, TransitionRecord } from "@loop-engine/core";
2
- export declare function replayLoop(definition: LoopDefinition, history: TransitionRecord[]): {
3
- valid: boolean;
4
- errors: string[];
5
- };
6
- //# sourceMappingURL=replay.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1E,wBAAgB,UAAU,CACxB,UAAU,EAAE,cAAc,EAC1B,OAAO,EAAE,gBAAgB,EAAE,GAC1B;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CActC"}
package/dist/replay.js DELETED
@@ -1,14 +0,0 @@
1
- export function replayLoop(definition, history) {
2
- const errors = [];
3
- let state = definition.initialState;
4
- for (const record of history) {
5
- const match = definition.transitions.find((t) => t.id === record.transitionId && t.from === state && t.to === record.toState);
6
- if (!match) {
7
- errors.push(`Invalid transition ${record.transitionId} from ${state} to ${record.toState}`);
8
- continue;
9
- }
10
- state = record.toState;
11
- }
12
- return { valid: errors.length === 0, errors };
13
- }
14
- //# sourceMappingURL=replay.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,UAAU,CACxB,UAA0B,EAC1B,OAA2B;IAE3B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,IAAI,KAAK,GAAG,UAAU,CAAC,YAAY,CAAC;IACpC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAG,UAAU,CAAC,WAAW,CAAC,IAAI,CACvC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,MAAM,CAAC,YAAY,IAAI,CAAC,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,CAAC,OAAO,CACnF,CAAC;QACF,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,CAAC,IAAI,CAAC,sBAAsB,MAAM,CAAC,YAAY,SAAS,KAAK,OAAO,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5F,SAAS;QACX,CAAC;QACD,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC;IACzB,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;AAChD,CAAC"}
@@ -1,18 +0,0 @@
1
- import type { AggregateId, LoopInstance, StateId, TransitionRecord } from "@loop-engine/core";
2
- export interface LoopTimeline {
3
- aggregateId: AggregateId;
4
- loopId: string;
5
- instance: LoopInstance;
6
- transitions: TransitionRecord[];
7
- durationMs: number;
8
- isComplete: boolean;
9
- }
10
- export interface StateResidency {
11
- stateId: StateId;
12
- enteredAt: string;
13
- exitedAt?: string;
14
- durationMs?: number;
15
- }
16
- export declare function buildTimeline(instance: LoopInstance, history: TransitionRecord[]): LoopTimeline;
17
- export declare function getStateResidency(timeline: LoopTimeline): StateResidency[];
18
- //# sourceMappingURL=timeline.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../src/timeline.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE9F,MAAM,WAAW,YAAY;IAC3B,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,YAAY,CAAC;IACvB,WAAW,EAAE,gBAAgB,EAAE,CAAC;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,YAAY,CAU/F;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,YAAY,GAAG,cAAc,EAAE,CAuB1E"}
package/dist/timeline.js DELETED
@@ -1,36 +0,0 @@
1
- export function buildTimeline(instance, history) {
2
- const end = instance.closedAt ?? new Date().toISOString();
3
- return {
4
- aggregateId: instance.aggregateId,
5
- loopId: instance.loopId,
6
- instance,
7
- transitions: [...history].sort((a, b) => Date.parse(a.occurredAt) - Date.parse(b.occurredAt)),
8
- durationMs: Math.max(0, Date.parse(end) - Date.parse(instance.startedAt)),
9
- isComplete: instance.status === "CLOSED"
10
- };
11
- }
12
- export function getStateResidency(timeline) {
13
- const out = [];
14
- let currentState = timeline.instance.currentState;
15
- let enteredAt = timeline.instance.startedAt;
16
- for (const t of timeline.transitions) {
17
- out.push({
18
- stateId: currentState,
19
- enteredAt,
20
- exitedAt: t.occurredAt,
21
- durationMs: Math.max(0, Date.parse(t.occurredAt) - Date.parse(enteredAt))
22
- });
23
- currentState = t.toState;
24
- enteredAt = t.occurredAt;
25
- }
26
- out.push({
27
- stateId: currentState,
28
- enteredAt,
29
- ...(timeline.instance.closedAt ? { exitedAt: timeline.instance.closedAt } : {}),
30
- ...(timeline.instance.closedAt
31
- ? { durationMs: Math.max(0, Date.parse(timeline.instance.closedAt) - Date.parse(enteredAt)) }
32
- : {})
33
- });
34
- return out;
35
- }
36
- //# sourceMappingURL=timeline.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"timeline.js","sourceRoot":"","sources":["../src/timeline.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,aAAa,CAAC,QAAsB,EAAE,OAA2B;IAC/E,MAAM,GAAG,GAAG,QAAQ,CAAC,QAAQ,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC1D,OAAO;QACL,WAAW,EAAE,QAAQ,CAAC,WAAW;QACjC,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,QAAQ;QACR,WAAW,EAAE,CAAC,GAAG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