@jcr-adk/state 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,15 @@
1
+ import type { AdkEventBase } from "@jcr-adk/core";
2
+ import type { EventEnvelope } from "./event-envelope.js";
3
+ import type { PersistedAdkEvent } from "./types.js";
4
+ export type AdkEventConversionResult = {
5
+ readonly ok: true;
6
+ readonly envelope: EventEnvelope;
7
+ } | {
8
+ readonly ok: false;
9
+ readonly message: string;
10
+ };
11
+ /** Convert domain AdkEventBase to on-disk EventEnvelope (F-01 shape). */
12
+ export declare function adkEventToEnvelope(event: AdkEventBase): AdkEventConversionResult;
13
+ /** Convert persisted envelope back to AdkEventBase. */
14
+ export declare function envelopeToAdkEvent(envelope: EventEnvelope, seq: number): PersistedAdkEvent;
15
+ //# sourceMappingURL=event-convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-convert.d.ts","sourceRoot":"","sources":["../src/event-convert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAa,MAAM,eAAe,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAa,MAAM,qBAAqB,CAAC;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AA8BpD,MAAM,MAAM,wBAAwB,GAChC;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAErD,yEAAyE;AACzE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,GAAG,wBAAwB,CA2DhF;AAED,uDAAuD;AACvD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CA4C1F"}
@@ -0,0 +1,135 @@
1
+ const KIND_TO_TYPE = {
2
+ "run.started": "run.started",
3
+ "run.completed": "run.completed",
4
+ "run.failed": "run.failed",
5
+ "tool.invoked": "tool.invoked",
6
+ "tool.completed": "tool.completed",
7
+ "policy.denied": "policy.denied",
8
+ "policy.approval.required": "policy.approval.required",
9
+ "policy.approved": "policy.approved",
10
+ "gate.report": "gate.completed",
11
+ "mutation.denied": "mutation.denied",
12
+ "tool.failed": "tool.failed",
13
+ };
14
+ const TYPE_TO_KIND = {
15
+ "run.started": "run.started",
16
+ "run.completed": "run.completed",
17
+ "run.failed": "run.failed",
18
+ "tool.invoked": "tool.invoked",
19
+ "tool.completed": "tool.completed",
20
+ "tool.failed": "tool.failed",
21
+ "policy.denied": "policy.denied",
22
+ "policy.approval.required": "policy.approval.required",
23
+ "policy.approved": "policy.approved",
24
+ "gate.completed": "gate.report",
25
+ "mutation.denied": "mutation.denied",
26
+ };
27
+ /** Convert domain AdkEventBase to on-disk EventEnvelope (F-01 shape). */
28
+ export function adkEventToEnvelope(event) {
29
+ const type = KIND_TO_TYPE[event.kind];
30
+ const base = {
31
+ timestamp: event.timestamp,
32
+ specId: event.specId,
33
+ runId: event.runId,
34
+ requestId: event.requestId,
35
+ type,
36
+ };
37
+ if (event.kind === "tool.completed") {
38
+ const toolName = stringField(event.payload, "toolName");
39
+ const toolCallId = stringField(event.payload, "toolCallId") ?? event.id;
40
+ if (!toolName)
41
+ return { ok: false, message: "tool.completed missing toolName in payload" };
42
+ const { toolName: _t, toolCallId: _c, ...rest } = event.payload;
43
+ return {
44
+ ok: true,
45
+ envelope: { ...base, type: "tool.completed", toolName, toolCallId, payload: rest },
46
+ };
47
+ }
48
+ if (event.kind === "tool.failed") {
49
+ const toolName = stringField(event.payload, "toolName");
50
+ const toolCallId = stringField(event.payload, "toolCallId") ?? event.id;
51
+ const error = stringField(event.payload, "error");
52
+ if (!toolName || !error) {
53
+ return { ok: false, message: "tool.failed missing toolName or error in payload" };
54
+ }
55
+ return { ok: true, envelope: { ...base, type: "tool.failed", toolName, toolCallId, error } };
56
+ }
57
+ if (event.kind === "policy.denied" ||
58
+ event.kind === "policy.approval.required" ||
59
+ event.kind === "policy.approved" ||
60
+ event.kind === "gate.report") {
61
+ const type = event.kind === "gate.report"
62
+ ? "gate.completed"
63
+ : event.kind;
64
+ return {
65
+ ok: true,
66
+ envelope: {
67
+ ...base,
68
+ type,
69
+ payload: { ...event.payload },
70
+ },
71
+ };
72
+ }
73
+ return {
74
+ ok: true,
75
+ envelope: {
76
+ ...base,
77
+ type,
78
+ payload: { ...event.payload },
79
+ },
80
+ };
81
+ }
82
+ /** Convert persisted envelope back to AdkEventBase. */
83
+ export function envelopeToAdkEvent(envelope, seq) {
84
+ const kind = TYPE_TO_KIND[envelope.type];
85
+ const id = inferEventId(envelope);
86
+ if (envelope.type === "tool.completed") {
87
+ return {
88
+ id,
89
+ kind,
90
+ timestamp: envelope.timestamp,
91
+ specId: envelope.specId,
92
+ runId: envelope.runId,
93
+ requestId: envelope.requestId,
94
+ payload: { toolName: envelope.toolName, toolCallId: envelope.toolCallId, ...envelope.payload },
95
+ seq,
96
+ };
97
+ }
98
+ if (envelope.type === "tool.failed") {
99
+ return {
100
+ id,
101
+ kind: "tool.failed",
102
+ timestamp: envelope.timestamp,
103
+ specId: envelope.specId,
104
+ runId: envelope.runId,
105
+ requestId: envelope.requestId,
106
+ payload: {
107
+ toolName: envelope.toolName,
108
+ toolCallId: envelope.toolCallId,
109
+ error: envelope.error,
110
+ },
111
+ seq,
112
+ };
113
+ }
114
+ return {
115
+ id,
116
+ kind,
117
+ timestamp: envelope.timestamp,
118
+ specId: envelope.specId,
119
+ runId: envelope.runId,
120
+ requestId: envelope.requestId,
121
+ payload: envelope.payload ?? {},
122
+ seq,
123
+ };
124
+ }
125
+ function inferEventId(envelope) {
126
+ if ("toolCallId" in envelope && typeof envelope.toolCallId === "string") {
127
+ return envelope.toolCallId;
128
+ }
129
+ return `${envelope.runId}-${envelope.type}-${envelope.timestamp}`;
130
+ }
131
+ function stringField(payload, key) {
132
+ const value = payload[key];
133
+ return typeof value === "string" && value.length > 0 ? value : undefined;
134
+ }
135
+ //# sourceMappingURL=event-convert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-convert.js","sourceRoot":"","sources":["../src/event-convert.ts"],"names":[],"mappings":"AAIA,MAAM,YAAY,GAAiC;IACjD,aAAa,EAAE,aAAa;IAC5B,eAAe,EAAE,eAAe;IAChC,YAAY,EAAE,YAAY;IAC1B,cAAc,EAAE,cAAc;IAC9B,gBAAgB,EAAE,gBAAgB;IAClC,eAAe,EAAE,eAAe;IAChC,0BAA0B,EAAE,0BAA0B;IACtD,iBAAiB,EAAE,iBAAiB;IACpC,aAAa,EAAE,gBAAgB;IAC/B,iBAAiB,EAAE,iBAAiB;IACpC,aAAa,EAAE,aAAa;CAC7B,CAAC;AAEF,MAAM,YAAY,GAAiC;IACjD,aAAa,EAAE,aAAa;IAC5B,eAAe,EAAE,eAAe;IAChC,YAAY,EAAE,YAAY;IAC1B,cAAc,EAAE,cAAc;IAC9B,gBAAgB,EAAE,gBAAgB;IAClC,aAAa,EAAE,aAAa;IAC5B,eAAe,EAAE,eAAe;IAChC,0BAA0B,EAAE,0BAA0B;IACtD,iBAAiB,EAAE,iBAAiB;IACpC,gBAAgB,EAAE,aAAa;IAC/B,iBAAiB,EAAE,iBAAiB;CACrC,CAAC;AAMF,yEAAyE;AACzE,MAAM,UAAU,kBAAkB,CAAC,KAAmB;IACpD,MAAM,IAAI,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG;QACX,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,KAAK,EAAE,KAAK,CAAC,KAAK;QACl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,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,60 @@
1
+ /** On-disk JSONL event types — F-01 compatible `type` field. */
2
+ export type EventType = "run.started" | "run.completed" | "run.failed" | "tool.invoked" | "tool.completed" | "tool.failed" | "policy.denied" | "policy.approval.required" | "policy.approved" | "gate.completed" | "mutation.denied";
3
+ export interface EventEnvelopeBase {
4
+ readonly timestamp: string;
5
+ readonly specId: string;
6
+ readonly runId: string;
7
+ readonly requestId: string;
8
+ readonly type: EventType;
9
+ }
10
+ export interface ToolCompletedEnvelope extends EventEnvelopeBase {
11
+ readonly type: "tool.completed";
12
+ readonly toolName: string;
13
+ readonly toolCallId: string;
14
+ readonly payload: Record<string, unknown>;
15
+ }
16
+ export interface ToolFailedEnvelope extends EventEnvelopeBase {
17
+ readonly type: "tool.failed";
18
+ readonly toolName: string;
19
+ readonly toolCallId: string;
20
+ readonly error: string;
21
+ }
22
+ export interface PolicyDeniedEnvelope extends EventEnvelopeBase {
23
+ readonly type: "policy.denied";
24
+ readonly payload: Record<string, unknown>;
25
+ }
26
+ export interface PolicyApprovalRequiredEnvelope extends EventEnvelopeBase {
27
+ readonly type: "policy.approval.required";
28
+ readonly payload: Record<string, unknown>;
29
+ }
30
+ export interface PolicyApprovedEnvelope extends EventEnvelopeBase {
31
+ readonly type: "policy.approved";
32
+ readonly payload: Record<string, unknown>;
33
+ }
34
+ export interface GateCompletedEnvelope extends EventEnvelopeBase {
35
+ readonly type: "gate.completed";
36
+ readonly payload: Record<string, unknown>;
37
+ }
38
+ export interface GenericEventEnvelope extends EventEnvelopeBase {
39
+ readonly type: Exclude<EventType, "tool.completed" | "tool.failed" | "policy.denied" | "policy.approval.required" | "policy.approved" | "gate.completed">;
40
+ readonly payload?: Record<string, unknown>;
41
+ readonly toolName?: string;
42
+ readonly toolCallId?: string;
43
+ readonly error?: string;
44
+ }
45
+ export type EventEnvelope = ToolCompletedEnvelope | ToolFailedEnvelope | PolicyDeniedEnvelope | PolicyApprovalRequiredEnvelope | PolicyApprovedEnvelope | GateCompletedEnvelope | GenericEventEnvelope;
46
+ export type EnvelopeValidationResult = {
47
+ readonly ok: true;
48
+ readonly envelope: EventEnvelope;
49
+ } | {
50
+ readonly ok: false;
51
+ readonly reason: "INVALID_SHAPE" | "UNKNOWN_TYPE";
52
+ readonly message: string;
53
+ };
54
+ /** Validate parsed JSON as EventEnvelope. */
55
+ export declare function validateEventEnvelope(raw: unknown): EnvelopeValidationResult;
56
+ /** Serialize envelope to a single JSONL line (no trailing newline). */
57
+ export declare function serializeEventEnvelope(envelope: EventEnvelope): string;
58
+ /** Parse one JSONL line — empty lines are invalid. */
59
+ export declare function parseEventLine(line: string): EnvelopeValidationResult;
60
+ //# sourceMappingURL=event-envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-envelope.d.ts","sourceRoot":"","sources":["../src/event-envelope.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,MAAM,MAAM,SAAS,GACjB,aAAa,GACb,eAAe,GACf,YAAY,GACZ,cAAc,GACd,gBAAgB,GAChB,aAAa,GACb,eAAe,GACf,0BAA0B,GAC1B,iBAAiB,GACjB,gBAAgB,GAChB,iBAAiB,CAAC;AAEtB,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;CAC1B;AAED,MAAM,WAAW,qBAAsB,SAAQ,iBAAiB;IAC9D,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAA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,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,MAAM,aAAa,GACrB,qBAAqB,GACrB,kBAAkB,GAClB,oBAAoB,GACpB,8BAA8B,GAC9B,sBAAsB,GACtB,qBAAqB,GACrB,oBAAoB,CAAC;AAEzB,MAAM,MAAM,wBAAwB,GAChC;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,eAAe,GAAG,cAAc,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAyBxG,6CAA6C;AAC7C,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,wBAAwB,CAsE5E;AAED,uEAAuE;AACvE,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,aAAa,GAAG,MAAM,CAEtE;AAED,sDAAsD;AACtD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,wBAAwB,CAUrE"}
@@ -0,0 +1,96 @@
1
+ const EVENT_TYPES = [
2
+ "run.started",
3
+ "run.completed",
4
+ "run.failed",
5
+ "tool.invoked",
6
+ "tool.completed",
7
+ "tool.failed",
8
+ "policy.denied",
9
+ "policy.approval.required",
10
+ "policy.approved",
11
+ "gate.completed",
12
+ "mutation.denied",
13
+ ];
14
+ function isRecord(value) {
15
+ return typeof value === "object" && value !== null && !Array.isArray(value);
16
+ }
17
+ function requireString(obj, key) {
18
+ const value = obj[key];
19
+ return typeof value === "string" && value.length > 0 ? value : null;
20
+ }
21
+ /** Validate parsed JSON as EventEnvelope. */
22
+ export function validateEventEnvelope(raw) {
23
+ if (!isRecord(raw)) {
24
+ return { ok: false, reason: "INVALID_SHAPE", message: "event must be an object" };
25
+ }
26
+ const timestamp = requireString(raw, "timestamp");
27
+ const specId = requireString(raw, "specId");
28
+ const runId = requireString(raw, "runId");
29
+ const requestId = requireString(raw, "requestId");
30
+ const type = requireString(raw, "type");
31
+ if (!timestamp || !specId || !runId || !requestId || !type) {
32
+ return { ok: false, reason: "INVALID_SHAPE", message: "missing correlation or type fields" };
33
+ }
34
+ if (!EVENT_TYPES.includes(type)) {
35
+ return { ok: false, reason: "UNKNOWN_TYPE", message: `unknown event type: ${type}` };
36
+ }
37
+ const base = { timestamp, specId, runId, requestId, type: type };
38
+ if (type === "tool.completed") {
39
+ const toolName = requireString(raw, "toolName");
40
+ const toolCallId = requireString(raw, "toolCallId");
41
+ const payload = raw.payload;
42
+ if (!toolName || !toolCallId || !isRecord(payload)) {
43
+ return { ok: false, reason: "INVALID_SHAPE", message: "tool.completed requires toolName, toolCallId, payload" };
44
+ }
45
+ return { ok: true, envelope: { ...base, type: "tool.completed", toolName, toolCallId, payload } };
46
+ }
47
+ if (type === "tool.failed") {
48
+ const toolName = requireString(raw, "toolName");
49
+ const toolCallId = requireString(raw, "toolCallId");
50
+ const error = requireString(raw, "error");
51
+ if (!toolName || !toolCallId || !error) {
52
+ return { ok: false, reason: "INVALID_SHAPE", message: "tool.failed requires toolName, toolCallId, error" };
53
+ }
54
+ return { ok: true, envelope: { ...base, type: "tool.failed", toolName, toolCallId, error } };
55
+ }
56
+ if (type === "policy.denied" ||
57
+ type === "policy.approval.required" ||
58
+ type === "policy.approved" ||
59
+ type === "gate.completed") {
60
+ const payload = raw.payload;
61
+ if (!isRecord(payload)) {
62
+ return { ok: false, reason: "INVALID_SHAPE", message: `${type} requires payload object` };
63
+ }
64
+ return {
65
+ ok: true,
66
+ envelope: { ...base, type, payload },
67
+ };
68
+ }
69
+ const envelope = {
70
+ ...base,
71
+ type: type,
72
+ payload: isRecord(raw.payload) ? raw.payload : undefined,
73
+ toolName: typeof raw.toolName === "string" ? raw.toolName : undefined,
74
+ toolCallId: typeof raw.toolCallId === "string" ? raw.toolCallId : undefined,
75
+ error: typeof raw.error === "string" ? raw.error : undefined,
76
+ };
77
+ return { ok: true, envelope };
78
+ }
79
+ /** Serialize envelope to a single JSONL line (no trailing newline). */
80
+ export function serializeEventEnvelope(envelope) {
81
+ return JSON.stringify(envelope);
82
+ }
83
+ /** Parse one JSONL line — empty lines are invalid. */
84
+ export function parseEventLine(line) {
85
+ const trimmed = line.trim();
86
+ if (!trimmed) {
87
+ return { ok: false, reason: "INVALID_SHAPE", message: "empty line" };
88
+ }
89
+ try {
90
+ return validateEventEnvelope(JSON.parse(trimmed));
91
+ }
92
+ catch {
93
+ return { ok: false, reason: "INVALID_SHAPE", message: "invalid JSON" };
94
+ }
95
+ }
96
+ //# sourceMappingURL=event-envelope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-envelope.js","sourceRoot":"","sources":["../src/event-envelope.ts"],"names":[],"mappings":"AAqFA,MAAM,WAAW,GAAyB;IACxC,aAAa;IACb,eAAe;IACf,YAAY;IACZ,cAAc;IACd,gBAAgB;IAChB,aAAa;IACb,eAAe;IACf,0BAA0B;IAC1B,iBAAiB;IACjB,gBAAgB;IAChB,iBAAiB;CAClB,CAAC;AAEF,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,aAAa,CAAC,GAA4B,EAAE,GAAW;IAC9D,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACtE,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,qBAAqB,CAAC,GAAY;I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OAAO,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;IACzE,CAAC;AACH,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { AdkEventBase } from "@jcr-adk/core";
2
+ import type { AppendOutcome, EventStore, PersistedAdkEvent } from "./types.js";
3
+ /** In-memory EventStore for unit tests and local dev. */
4
+ export declare class InMemoryEventStore implements EventStore {
5
+ private seq;
6
+ private events;
7
+ private readonly boundRunId?;
8
+ constructor(boundRunId?: string);
9
+ append(event: AdkEventBase): Promise<AppendOutcome>;
10
+ readAll(): Promise<PersistedAdkEvent[]>;
11
+ readByRun(runId: string): AsyncGenerator<PersistedAdkEvent>;
12
+ }
13
+ //# sourceMappingURL=in-memory-event-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory-event-store.d.ts","sourceRoot":"","sources":["../src/in-memory-event-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EACV,aAAa,EACb,UAAU,EACV,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,yDAAyD;AACzD,qBAAa,kBAAmB,YAAW,UAAU;IACnD,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAS;gBAEzB,UAAU,CAAC,EAAE,MAAM;IAIzB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IA+BnD,OAAO,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAItC,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAAC,iBAAiB,CAAC;CAKnE"}
@@ -0,0 +1,54 @@
1
+ /** In-memory EventStore for unit tests and local dev. */
2
+ export class InMemoryEventStore {
3
+ seq = 0;
4
+ events = [];
5
+ boundRunId;
6
+ constructor(boundRunId) {
7
+ this.boundRunId = boundRunId;
8
+ }
9
+ async append(event) {
10
+ if (!isValidEvent(event)) {
11
+ return {
12
+ ok: false,
13
+ reason: "INVALID_EVENT",
14
+ message: "missing required event fields",
15
+ };
16
+ }
17
+ if (this.boundRunId && event.runId !== this.boundRunId) {
18
+ return {
19
+ ok: false,
20
+ reason: "RUN_ID_MISMATCH",
21
+ message: `runId mismatch: expected ${this.boundRunId}`,
22
+ };
23
+ }
24
+ if (this.events.length > 0 && event.runId !== this.events[0].runId) {
25
+ return {
26
+ ok: false,
27
+ reason: "RUN_ID_MISMATCH",
28
+ message: "single-runId store rejects mixed runId",
29
+ };
30
+ }
31
+ this.seq += 1;
32
+ const persisted = { ...event, seq: this.seq };
33
+ this.events.push(persisted);
34
+ return { ok: true, event: persisted };
35
+ }
36
+ async readAll() {
37
+ return [...this.events];
38
+ }
39
+ async *readByRun(runId) {
40
+ for (const event of this.events) {
41
+ if (event.runId === runId)
42
+ yield event;
43
+ }
44
+ }
45
+ }
46
+ function isValidEvent(event) {
47
+ return Boolean(event.id &&
48
+ event.kind &&
49
+ event.timestamp &&
50
+ event.specId &&
51
+ event.runId &&
52
+ event.requestId);
53
+ }
54
+ //# sourceMappingURL=in-memory-event-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory-event-store.js","sourceRoot":"","sources":["../src/in-memory-event-store.ts"],"names":[],"mappings":"AAOA,yDAAyD;AACzD,MAAM,OAAO,kBAAkB;IACrB,GAAG,GAAG,CAAC,CAAC;IACR,MAAM,GAAwB,EAAE,CAAC;IACxB,UAAU,CAAU;IAErC,YAAY,UAAmB;QAC7B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAAmB;QAC9B,IAAI,CAAC,YAAY,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OAAO,CACZ,KAAK,CAAC,EAAE;QACN,KAAK,CAAC,IAAI;QACV,KAAK,CAAC,SAAS;QACf,KAAK,CAAC,MAAM;QACZ,KAAK,CAAC,KAAK;QACX,KAAK,CAAC,SAAS,CAClB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,12 @@
1
+ export type { PersistedAdkEvent, AppendResult, AppendFailure, AppendOutcome, EventStore, } from "./types.js";
2
+ export { EVENT_FIRST_WRITE_ORDERING } from "./types.js";
3
+ export type { EventType, EventEnvelope, EventEnvelopeBase, ToolCompletedEnvelope, ToolFailedEnvelope, PolicyDeniedEnvelope, GateCompletedEnvelope, EnvelopeValidationResult, } from "./event-envelope.js";
4
+ export { validateEventEnvelope, serializeEventEnvelope, parseEventLine, } from "./event-envelope.js";
5
+ export { adkEventToEnvelope, envelopeToAdkEvent } from "./event-convert.js";
6
+ export type { AdkEventConversionResult } from "./event-convert.js";
7
+ export { JsonlWriter } from "./jsonl-writer.js";
8
+ export type { JsonlReadResult, JsonlAppendResult } from "./jsonl-writer.js";
9
+ export { JsonlEventStore } from "./jsonl-event-store.js";
10
+ export type { JsonlEventStoreOptions } from "./jsonl-event-store.js";
11
+ export { InMemoryEventStore } from "./in-memory-event-store.js";
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,aAAa,EACb,UAAU,GACX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAExD,YAAY,EACV,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,qBAAqB,EACrB,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,cAAc,GACf,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC5E,YAAY,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAE5E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,YAAY,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAErE,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { EVENT_FIRST_WRITE_ORDERING } from "./types.js";
2
+ export { validateEventEnvelope, serializeEventEnvelope, parseEventLine, } from "./event-envelope.js";
3
+ export { adkEventToEnvelope, envelopeToAdkEvent } from "./event-convert.js";
4
+ export { JsonlWriter } from "./jsonl-writer.js";
5
+ export { JsonlEventStore } from "./jsonl-event-store.js";
6
+ export { InMemoryEventStore } from "./in-memory-event-store.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAYxD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,cAAc,GACf,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAG5E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,37 @@
1
+ import type { AdkEventBase } from "@jcr-adk/core";
2
+ import type { EventEnvelope } from "./event-envelope.js";
3
+ import type { AppendOutcome, EventStore, PersistedAdkEvent } from "./types.js";
4
+ export interface JsonlEventStoreOptions {
5
+ readonly filePath: string;
6
+ /** When set, rejects append if runId differs from file or bound value. */
7
+ readonly boundRunId?: string;
8
+ }
9
+ /**
10
+ * File-backed append-only EventStore.
11
+ *
12
+ * Fix 2: in-memory seq/runId cache — O(1) append after first write.
13
+ * - On first append, file is read once to replay existing seq/runId.
14
+ * - Subsequent appends use cached values; no extra readAll().
15
+ * - Cache is invalidated if an external write corrupts the file;
16
+ * callers must construct a fresh instance in that case.
17
+ *
18
+ * Fix 4: JsonlWriter.INVALID_LINE → AppendOutcome.INVALID_EVENT mapping.
19
+ * - Serialisation failures are now reported as INVALID_EVENT, not CORRUPT_LINE.
20
+ */
21
+ export declare class JsonlEventStore implements EventStore {
22
+ private readonly writer;
23
+ private readonly boundRunId?;
24
+ /** Cached after first interaction with the file. */
25
+ private cachedSeq;
26
+ private cachedRunId;
27
+ private cacheInitialised;
28
+ constructor(options: JsonlEventStoreOptions);
29
+ get filePath(): string;
30
+ /** Initialise cache from existing file content (called once). */
31
+ private initCache;
32
+ append(event: AdkEventBase): Promise<AppendOutcome>;
33
+ appendEnvelope(envelope: EventEnvelope): Promise<AppendOutcome>;
34
+ readAll(): Promise<PersistedAdkEvent[]>;
35
+ readByRun(runId: string): AsyncGenerator<PersistedAdkEvent>;
36
+ }
37
+ //# sourceMappingURL=jsonl-event-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-event-store.d.ts","sourceRoot":"","sources":["../src/jsonl-event-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGzD,OAAO,KAAK,EACV,aAAa,EACb,UAAU,EACV,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,0EAA0E;IAC1E,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,eAAgB,YAAW,UAAU;IAChD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAc;IACrC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAS;IAErC,oDAAoD;IACpD,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,gBAAgB,CAAS;gBAErB,OAAO,EAAE,sBAAsB;IAK3C,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,iEAAiE;YACnD,SAAS;IAUjB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAQnD,cAAc,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAgC/D,OAAO,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAQtC,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAAC,iBAAiB,CAAC;CAMnE"}
@@ -0,0 +1,89 @@
1
+ import { adkEventToEnvelope, envelopeToAdkEvent } from "./event-convert.js";
2
+ import { JsonlWriter } from "./jsonl-writer.js";
3
+ /**
4
+ * File-backed append-only EventStore.
5
+ *
6
+ * Fix 2: in-memory seq/runId cache — O(1) append after first write.
7
+ * - On first append, file is read once to replay existing seq/runId.
8
+ * - Subsequent appends use cached values; no extra readAll().
9
+ * - Cache is invalidated if an external write corrupts the file;
10
+ * callers must construct a fresh instance in that case.
11
+ *
12
+ * Fix 4: JsonlWriter.INVALID_LINE → AppendOutcome.INVALID_EVENT mapping.
13
+ * - Serialisation failures are now reported as INVALID_EVENT, not CORRUPT_LINE.
14
+ */
15
+ export class JsonlEventStore {
16
+ writer;
17
+ boundRunId;
18
+ /** Cached after first interaction with the file. */
19
+ cachedSeq = null;
20
+ cachedRunId = null;
21
+ cacheInitialised = false;
22
+ constructor(options) {
23
+ this.writer = new JsonlWriter(options.filePath);
24
+ this.boundRunId = options.boundRunId;
25
+ }
26
+ get filePath() {
27
+ return this.writer.path;
28
+ }
29
+ /** Initialise cache from existing file content (called once). */
30
+ async initCache() {
31
+ const existing = await this.writer.readAll();
32
+ if (!existing.ok)
33
+ return existing;
34
+ this.cachedSeq = existing.events.length;
35
+ this.cachedRunId = existing.events[0]?.runId ?? null;
36
+ this.cacheInitialised = true;
37
+ return { ok: true };
38
+ }
39
+ async append(event) {
40
+ const converted = adkEventToEnvelope(event);
41
+ if (!converted.ok) {
42
+ return { ok: false, reason: "INVALID_EVENT", message: converted.message };
43
+ }
44
+ return this.appendEnvelope(converted.envelope);
45
+ }
46
+ async appendEnvelope(envelope) {
47
+ if (!this.cacheInitialised) {
48
+ const init = await this.initCache();
49
+ if (!init.ok)
50
+ return init;
51
+ }
52
+ const expectedRunId = this.boundRunId ?? this.cachedRunId;
53
+ if (expectedRunId !== null && envelope.runId !== expectedRunId) {
54
+ return {
55
+ ok: false,
56
+ reason: "RUN_ID_MISMATCH",
57
+ message: `runId mismatch: expected ${expectedRunId}, got ${envelope.runId}`,
58
+ };
59
+ }
60
+ const written = await this.writer.append(envelope);
61
+ if (!written.ok) {
62
+ return {
63
+ ok: false,
64
+ reason: written.reason === "IO_ERROR" ? "IO_ERROR" : "INVALID_EVENT",
65
+ message: written.message,
66
+ };
67
+ }
68
+ this.cachedSeq = (this.cachedSeq ?? 0) + 1;
69
+ if (this.cachedRunId === null) {
70
+ this.cachedRunId = envelope.runId;
71
+ }
72
+ return { ok: true, event: envelopeToAdkEvent(envelope, this.cachedSeq) };
73
+ }
74
+ async readAll() {
75
+ const result = await this.writer.readAll();
76
+ if (!result.ok) {
77
+ throw new Error(`failed to read events: ${result.message}`);
78
+ }
79
+ return result.events.map((envelope, index) => envelopeToAdkEvent(envelope, index + 1));
80
+ }
81
+ async *readByRun(runId) {
82
+ const all = await this.readAll();
83
+ for (const event of all) {
84
+ if (event.runId === runId)
85
+ yield event;
86
+ }
87
+ }
88
+ }
89
+ //# sourceMappingURL=jsonl-event-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-event-store.js","sourceRoot":"","sources":["../src/jsonl-event-store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAahD;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,eAAe;IACT,MAAM,CAAc;IACpB,UAAU,CAAU;IAErC,oDAAoD;IAC5C,SAAS,GAAkB,IAAI,CAAC;IAChC,WAAW,GAAkB,IAAI,CAAC;IAClC,gBAAgB,GAAG,KAAK,CAAC;IAEjC,YAAY,OAA+B;QACzC,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACvC,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED,iEAAiE;IACzD,KAAK,CAAC,SAAS;QACrB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC7C,IAAI,CAAC,QAAQ,CAAC,EAAE;YAAE,OAAO,QAAQ,CAAC;QAElC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;QACxC,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,IAAI,CAAC;QACrD,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC7B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAA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AAC,GAAG,CAAC,CAAC;QAC3C,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI,EAAE,CAAC;YAC9B,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,KAAK,CAAC;QACpC,CAAC;QAED,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,kBAAkB,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;IAC3E,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC3C,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9D,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC,kBAAkB,CAAC,QAAQ,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IACzF,CAAC;IAED,KAAK,CAAC,CAAC,SAAS,CAAC,KAAa;QAC5B,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACjC,KAAK,MAAM,KAAK,IAAI,GAAG,EAAE,CAAC;YACxB,IAAI,KAAK,CAAC,KAAK,KAAK,KAAK;gBAAE,MAAM,KAAK,CAAC;QACzC,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,30 @@
1
+ import { type EventEnvelope } from "./event-envelope.js";
2
+ export type JsonlReadResult = {
3
+ readonly ok: true;
4
+ readonly events: EventEnvelope[];
5
+ } | {
6
+ readonly ok: false;
7
+ readonly reason: "IO_ERROR" | "CORRUPT_LINE";
8
+ readonly message: string;
9
+ readonly lineNumber?: number;
10
+ };
11
+ export type JsonlAppendResult = {
12
+ readonly ok: true;
13
+ } | {
14
+ readonly ok: false;
15
+ readonly reason: "IO_ERROR" | "INVALID_LINE" | "CORRUPT_FILE";
16
+ readonly message: string;
17
+ };
18
+ /** Append-only JSONL writer with line validation. */
19
+ export declare class JsonlWriter {
20
+ private readonly filePath;
21
+ constructor(filePath: string);
22
+ get path(): string;
23
+ /** Read and validate all lines — fail-closed on first corrupt line. */
24
+ readAll(): Promise<JsonlReadResult>;
25
+ /** Append one validated line — writes full line + newline via appendFile. */
26
+ append(envelope: EventEnvelope): Promise<JsonlAppendResult>;
27
+ /** Replace file contents — test helper only. */
28
+ writeRaw(content: string): Promise<void>;
29
+ }
30
+ //# sourceMappingURL=jsonl-writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-writer.d.ts","sourceRoot":"","sources":["../src/jsonl-writer.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,aAAa,EACnB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,cAAc,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjI,MAAM,MAAM,iBAAiB,GACzB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;CAAE,GACrB;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,cAAc,GAAG,cAAc,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEpH,qDAAqD;AACrD,qBAAa,WAAW;IACV,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,MAAM;IAE7C,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,uEAAuE;IACjE,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC;IA6BzC,6EAA6E;IACvE,MAAM,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAgBjE,gDAAgD;IAC1C,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAI/C"}
@@ -0,0 +1,69 @@
1
+ import { appendFile, mkdir, readFile, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { parseEventLine, serializeEventEnvelope, } from "./event-envelope.js";
4
+ /** Append-only JSONL writer with line validation. */
5
+ export class JsonlWriter {
6
+ filePath;
7
+ constructor(filePath) {
8
+ this.filePath = filePath;
9
+ }
10
+ get path() {
11
+ return this.filePath;
12
+ }
13
+ /** Read and validate all lines — fail-closed on first corrupt line. */
14
+ async readAll() {
15
+ let raw;
16
+ try {
17
+ raw = await readFile(this.filePath, "utf8");
18
+ }
19
+ catch (error) {
20
+ if (isEnoent(error)) {
21
+ return { ok: true, events: [] };
22
+ }
23
+ return { ok: false, reason: "IO_ERROR", message: String(error) };
24
+ }
25
+ const lines = raw.split("\n").filter((line) => line.length > 0);
26
+ const events = [];
27
+ for (let i = 0; i < lines.length; i += 1) {
28
+ const parsed = parseEventLine(lines[i]);
29
+ if (!parsed.ok) {
30
+ return {
31
+ ok: false,
32
+ reason: "CORRUPT_LINE",
33
+ message: parsed.message,
34
+ lineNumber: i + 1,
35
+ };
36
+ }
37
+ events.push(parsed.envelope);
38
+ }
39
+ return { ok: true, events };
40
+ }
41
+ /** Append one validated line — writes full line + newline via appendFile. */
42
+ async append(envelope) {
43
+ const line = serializeEventEnvelope(envelope);
44
+ const parsed = parseEventLine(line);
45
+ if (!parsed.ok) {
46
+ return { ok: false, reason: "INVALID_LINE", message: parsed.message };
47
+ }
48
+ try {
49
+ await mkdir(path.dirname(this.filePath), { recursive: true });
50
+ await appendFile(this.filePath, `${line}\n`, "utf8");
51
+ return { ok: true };
52
+ }
53
+ catch (error) {
54
+ return { ok: false, reason: "IO_ERROR", message: String(error) };
55
+ }
56
+ }
57
+ /** Replace file contents — test helper only. */
58
+ async writeRaw(content) {
59
+ await mkdir(path.dirname(this.filePath), { recursive: true });
60
+ await writeFile(this.filePath, content, "utf8");
61
+ }
62
+ }
63
+ function isEnoent(error) {
64
+ return isRecord(error) && "code" in error && error.code === "ENOENT";
65
+ }
66
+ function isRecord(value) {
67
+ return typeof value === "object" && value !== null;
68
+ }
69
+ //# sourceMappingURL=jsonl-writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-writer.js","sourceRoot":"","sources":["../src/jsonl-writer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,cAAc,EACd,sBAAsB,GAEvB,MAAM,qBAAqB,CAAC;AAU7B,qDAAqD;AACrD,MAAM,OAAO,WAAW;IACO;IAA7B,YAA6B,QAAgB;QAAhB,aAAQ,GAAR,QAAQ,CAAQ;IAAG,CAAC;IAEjD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,uEAAuE;IACvE,KAAK,CAAC,OAAO;QACX,IAAI,GAAW,CAAC;QAChB,IAAI,CAAC;YACH,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC9C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;gBACpB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;YAClC,CAAC;YACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,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,CAAC,CAAC;YAC9D,MAAM,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,IAAI,IAAI,EAAE,MAAM,CAAC,CAAC;YACrD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;QACtB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QACnE,CAAC;IACH,CAAC;IAED,gDAAgD;IAChD,KAAK,CAAC,QAAQ,CAAC,OAAe;QAC5B,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC9D,MAAM,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAClD,CAAC;CACF;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;AACvE,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AACrD,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { AdkEventBase } from "@jcr-adk/core";
2
+ /** Persisted event — adds sequence for JSONL ordering. Owned by state. */
3
+ export interface PersistedAdkEvent extends AdkEventBase {
4
+ readonly seq: number;
5
+ }
6
+ export interface AppendResult {
7
+ readonly ok: true;
8
+ readonly event: PersistedAdkEvent;
9
+ }
10
+ export interface AppendFailure {
11
+ readonly ok: false;
12
+ readonly reason: "IO_ERROR" | "CORRUPT_LINE" | "INVALID_EVENT" | "RUN_ID_MISMATCH";
13
+ readonly message: string;
14
+ }
15
+ export type AppendOutcome = AppendResult | AppendFailure;
16
+ /**
17
+ * EventStore contract — append fail-closed on IO/corrupt/runId mismatch.
18
+ * Callers must not mark mutations successful until append returns ok.
19
+ */
20
+ export interface EventStore {
21
+ append(event: AdkEventBase): Promise<AppendOutcome>;
22
+ readAll(): Promise<PersistedAdkEvent[]>;
23
+ readByRun(runId: string): AsyncGenerator<PersistedAdkEvent>;
24
+ }
25
+ /** Write ordering rule for gateway/state integration. */
26
+ export declare const EVENT_FIRST_WRITE_ORDERING: "mutation-pending-until-event-append";
27
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,0EAA0E;AAC1E,MAAM,WAAW,iBAAkB,SAAQ,YAAY;IACrD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,iBAAiB,CAAC;CACnC;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,MAAM,EACX,UAAU,GACV,cAAc,GACd,eAAe,GACf,iBAAiB,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG,aAAa,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACpD,OAAO,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;IACxC,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAAC,iBAAiB,CAAC,CAAC;CAC7D;AAED,yDAAyD;AACzD,eAAO,MAAM,0BAA0B,EACrC,qCAA8C,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ /** Write ordering rule for gateway/state integration. */
2
+ export const EVENT_FIRST_WRITE_ORDERING = "mutation-pending-until-event-append";
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAkCA,yDAAyD;AACzD,MAAM,CAAC,MAAM,0BAA0B,GACrC,qCAA8C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@jcr-adk/state",
3
+ "version": "2.0.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.js"
9
+ }
10
+ },
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "dependencies": {
14
+ "@jcr-adk/core": "2.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "tsx": "^4.19.4",
18
+ "typescript": "^5.8.3"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "description": "JCR ADK (Jason Cursor ADK) — state",
27
+ "scripts": {
28
+ "typecheck": "tsc --noEmit",
29
+ "test": "node --import tsx --test src/in-memory-event-store.test.ts src/jsonl-event-store.test.ts",
30
+ "build": "tsc -p tsconfig.build.json"
31
+ }
32
+ }