@kindgi/audit-events 0.0.0-bootstrap.0 → 0.1.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 +201 -0
- package/README.md +88 -2
- package/dist/binding.d.ts +52 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/binding.js +4 -0
- package/dist/binding.js.map +1 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +121 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -4
- package/src/binding.ts +63 -0
- package/src/errors.ts +28 -0
- package/src/index.ts +16 -0
- package/src/types.ts +126 -0
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 describing the origin of the Work and
|
|
141
|
+
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 2026 Kindgi Inc.
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/audit-events`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type contract for the Kindgi audit log. Defines the `AuditEvent` record every subsystem emits (authorization decisions, secret and env changes, run outcomes, guardrail violations, human-in-the-loop decisions) and `AuditEventBinding`, the tenant-scoped append / query / purge interface that storage adapters implement. Types only; this package has no runtime code.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Give the whole system one audit substrate: one record shape, one write surface, one query surface. Compliance evidence is a classification lens over this stream (see [`@kindgi/compliance`](../compliance/)), not a second write path. Common fields (`tenantId`, `projectId`, `timestamp`, `actor`, `correlationId`, `runId`, …) are top-level so they can be indexed; kind-specific data lives in a versioned `payload`. The binding is caller-plugged: [`@kindgi/api`](../api/) takes one as `auditEvents` and mounts the audit routes over it, and the reference in-memory implementation lives in [`@kindgi/audit-events-inmemory`](../audit-events-inmemory/).
|
|
8
|
+
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
- **`AuditEvent`** — one audit record:
|
|
12
|
+
- `id` — caller-supplied, unique per tenant.
|
|
13
|
+
- `tenantId`, `projectId?` (absent for tenant-level events), `kind` (open string, e.g. `'authz-decision'`, `'secret-set'`, `'run-outcome'`), `timestamp`.
|
|
14
|
+
- `actor` — `<type>:<id>`, e.g. `user:u-1042`, `agent:support-triage`, `user:system` for framework-internal actions. `onBehalfOf?` names the delegating party.
|
|
15
|
+
- `correlationId?`, `runId?`, `agentId?`, `flowId?`, `outcome?`.
|
|
16
|
+
- `payload` — versioned envelope `{ v: 1, doc: … }`; the `doc` shape depends on `kind`.
|
|
17
|
+
- **`AuditEventBinding`** — the adapter interface:
|
|
18
|
+
- `append(events)` — batched write. Duplicate `(tenantId, id)` pairs are ignored, so retries are safe. Fails with `PersistenceError` or `AuditEventValidationError`.
|
|
19
|
+
- `query(input)` — cursor-paginated read ordered by `(timestamp, id)`; the cursor is opaque. Fails with `PersistenceError` or `InvalidCursorError`.
|
|
20
|
+
- `purge(input)` — delete one kind's events older than a cutoff; returns the deleted count.
|
|
21
|
+
- `describe()` — `{ name, version }`.
|
|
22
|
+
- **`AuditEventQueryInput`** — `tenantId`, `filter?`, `cursor?`, `limit?`.
|
|
23
|
+
- **`AuditEventFilter`** — AND-combined; absent fields match anything. Fields: `id`, `kind`, `kinds` (an empty array matches nothing), `actor`, `onBehalfOf`, `runId`, `agentId`, `flowId`, `correlationId`, `outcome`, `payloadDoc` (every entry must equal the same field of `payload.doc`), and inclusive ISO `from` / `to` bounds.
|
|
24
|
+
- **`AuditEventPage`** — `data` plus `nextCursor` (absent on the last page).
|
|
25
|
+
- **`AuditEventPurgeInput`** (`tenantId`, `kind`, `olderThan`) and **`AuditEventPurgeResult`** (`deleted`).
|
|
26
|
+
- **`AuditEventError`** — union of **`PersistenceError`** (`persistence-error`), **`InvalidCursorError`** (`invalid-cursor`), and **`AuditEventValidationError`** (`invalid-event`, with optional per-path `issues`).
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { randomUUID } from 'node:crypto';
|
|
32
|
+
|
|
33
|
+
import type { AuditEvent, AuditEventBinding } from '@kindgi/audit-events';
|
|
34
|
+
import type { ProjectId, TenantId, Timestamp } from '@kindgi/types';
|
|
35
|
+
|
|
36
|
+
async function recordDecision(
|
|
37
|
+
audit: AuditEventBinding,
|
|
38
|
+
tenantId: TenantId,
|
|
39
|
+
projectId: ProjectId,
|
|
40
|
+
allowed: boolean,
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
const event: AuditEvent = {
|
|
43
|
+
id: randomUUID(), // unique per tenant; re-appending the same id is a no-op
|
|
44
|
+
tenantId,
|
|
45
|
+
projectId,
|
|
46
|
+
kind: 'authz-decision',
|
|
47
|
+
timestamp: new Date().toISOString() as Timestamp,
|
|
48
|
+
actor: 'agent:support-triage',
|
|
49
|
+
onBehalfOf: 'user:u-1042',
|
|
50
|
+
outcome: allowed ? 'allowed' : 'denied',
|
|
51
|
+
payload: { v: 1, doc: { action: 'tool:invoke', resource: 'crm.lookup' } },
|
|
52
|
+
};
|
|
53
|
+
const written = await audit.append([event]);
|
|
54
|
+
if (written.kind === 'err') throw new Error(`${written.error.code}: ${written.error.message}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Page through every denial since `from`, oldest first.
|
|
58
|
+
async function* denialsSince(
|
|
59
|
+
audit: AuditEventBinding,
|
|
60
|
+
tenantId: TenantId,
|
|
61
|
+
from: string,
|
|
62
|
+
): AsyncGenerator<AuditEvent> {
|
|
63
|
+
let cursor: string | undefined;
|
|
64
|
+
do {
|
|
65
|
+
const page = await audit.query({
|
|
66
|
+
tenantId,
|
|
67
|
+
filter: { kind: 'authz-decision', outcome: 'denied', from },
|
|
68
|
+
limit: 100,
|
|
69
|
+
...(cursor !== undefined && { cursor }),
|
|
70
|
+
});
|
|
71
|
+
if (page.kind === 'err') throw new Error(`${page.error.code}: ${page.error.message}`);
|
|
72
|
+
yield* page.value.data;
|
|
73
|
+
cursor = page.value.nextCursor;
|
|
74
|
+
} while (cursor !== undefined);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Non-goals
|
|
79
|
+
|
|
80
|
+
- **No storage implementation.** Adapters implement `AuditEventBinding`; the in-memory reference is [`@kindgi/audit-events-inmemory`](../audit-events-inmemory/), and durable adapters are supplied by the Kindgi runtime.
|
|
81
|
+
- **No signing on the write path.** Records are stored unsigned; signatures are computed over canonical bytes at export time by the compliance layer.
|
|
82
|
+
- **No retention policy.** `purge` removes one kind older than a cutoff. Deciding when to call it, and exempting legal-hold kinds, belongs to the caller and the compliance classifier.
|
|
83
|
+
- **No closed set of kinds or payload schemas.** `kind` is an open string so packs can add their own; each kind defines its own `payload.doc`.
|
|
84
|
+
|
|
85
|
+
## Related
|
|
86
|
+
|
|
87
|
+
- [`@kindgi/audit-events-inmemory`](../audit-events-inmemory/) — reference `AuditEventBinding` for tests and local development.
|
|
88
|
+
- [`@kindgi/compliance`](../compliance/) — classifier and signed export over this stream; `auditEventToEvidence` maps an `AuditEvent` to an evidence record.
|
|
89
|
+
- [`@kindgi/api`](../api/) — accepts an `AuditEventBinding` as `auditEvents`.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Result, TenantId } from '@kindgi/types';
|
|
2
|
+
import type { AuditEventValidationError, InvalidCursorError, PersistenceError } from './errors.js';
|
|
3
|
+
import type { AuditEvent, AuditEventFilter, AuditEventPage } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Caller-plugged, tenant-scoped write + query surface for the unified
|
|
6
|
+
* audit substrate.
|
|
7
|
+
*
|
|
8
|
+
* Contracts:
|
|
9
|
+
* - `append` — batched write; groups by tenant, one insert per
|
|
10
|
+
* tenant per call. Duplicates on `(tenantId, id)` are silent
|
|
11
|
+
* (idempotent — safer than throwing when the caller re-fires
|
|
12
|
+
* from a retry loop).
|
|
13
|
+
* - `query` — cursor-paginated read; opaque cursor tokens encode
|
|
14
|
+
* `(timestamp, id)` for keyset pagination.
|
|
15
|
+
* - `purge` — delete rows matching `(tenantId, kind, olderThan)`;
|
|
16
|
+
* retention cleanup calls this per kind. Legal-hold kinds are
|
|
17
|
+
* excluded by the caller (the compliance classifier decides).
|
|
18
|
+
*
|
|
19
|
+
* Signing lives at the export layer (compliance export), not on the
|
|
20
|
+
* write path. Row content is stable; signature is deterministic from
|
|
21
|
+
* canonical bytes.
|
|
22
|
+
*/
|
|
23
|
+
export interface AuditEventBinding {
|
|
24
|
+
append(events: readonly AuditEvent[]): Promise<Result<void, PersistenceError | AuditEventValidationError>>;
|
|
25
|
+
query(input: AuditEventQueryInput): Promise<Result<AuditEventPage, PersistenceError | InvalidCursorError>>;
|
|
26
|
+
purge(input: AuditEventPurgeInput): Promise<Result<AuditEventPurgeResult, PersistenceError>>;
|
|
27
|
+
describe(): {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly version: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export interface AuditEventQueryInput {
|
|
33
|
+
readonly tenantId: TenantId;
|
|
34
|
+
readonly filter?: AuditEventFilter;
|
|
35
|
+
readonly cursor?: string;
|
|
36
|
+
readonly limit?: number;
|
|
37
|
+
}
|
|
38
|
+
export interface AuditEventPurgeInput {
|
|
39
|
+
readonly tenantId: TenantId;
|
|
40
|
+
/**
|
|
41
|
+
* Event kind to purge. One purge = one kind — keeps retention
|
|
42
|
+
* cleanup's per-kind granularity explicit. Legal-hold kinds should
|
|
43
|
+
* never reach this call.
|
|
44
|
+
*/
|
|
45
|
+
readonly kind: string;
|
|
46
|
+
/** ISO timestamp cutoff. Rows with `timestamp < olderThan` are deleted. */
|
|
47
|
+
readonly olderThan: string;
|
|
48
|
+
}
|
|
49
|
+
export interface AuditEventPurgeResult {
|
|
50
|
+
readonly deleted: number;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=binding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binding.d.ts","sourceRoot":"","sources":["../src/binding.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,KAAK,EAAE,yBAAyB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACnG,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,iBAAiB;IAChC,MAAM,CACJ,MAAM,EAAE,SAAS,UAAU,EAAE,GAC5B,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,GAAG,yBAAyB,CAAC,CAAC,CAAC;IAEvE,KAAK,CACH,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,gBAAgB,GAAG,kBAAkB,CAAC,CAAC,CAAC;IAE1E,KAAK,CAAC,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,MAAM,CAAC,qBAAqB,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAE7F,QAAQ,IAAI;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CACjE;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,MAAM,CAAC,EAAE,gBAAgB,CAAC;IACnC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,2EAA2E;IAC3E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B"}
|
package/dist/binding.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binding.js","sourceRoot":"","sources":["../src/binding.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface PersistenceError {
|
|
2
|
+
readonly code: 'persistence-error';
|
|
3
|
+
readonly message: string;
|
|
4
|
+
readonly cause?: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface InvalidCursorError {
|
|
7
|
+
readonly code: 'invalid-cursor';
|
|
8
|
+
readonly message: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Rejected on `append` when an event fails schema validation before
|
|
12
|
+
* write. `issues` is the per-path breakdown from the underlying
|
|
13
|
+
* validator. Exported as `AuditEventValidationError` to disambiguate
|
|
14
|
+
* from `@kindgi/schema.ValidationError` (a different shape used for
|
|
15
|
+
* JSON schema validation failures at the type-registry boundary).
|
|
16
|
+
*/
|
|
17
|
+
export interface AuditEventValidationError {
|
|
18
|
+
readonly code: 'invalid-event';
|
|
19
|
+
readonly message: string;
|
|
20
|
+
readonly issues?: readonly {
|
|
21
|
+
readonly path: string;
|
|
22
|
+
readonly message: string;
|
|
23
|
+
}[];
|
|
24
|
+
}
|
|
25
|
+
export type AuditEventError = PersistenceError | InvalidCursorError | AuditEventValidationError;
|
|
26
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAClF;AAED,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG,kBAAkB,GAAG,yBAAyB,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { AuditEventBinding, AuditEventPurgeInput, AuditEventPurgeResult, AuditEventQueryInput, } from './binding.js';
|
|
2
|
+
export type { AuditEventError, AuditEventValidationError, InvalidCursorError, PersistenceError, } from './errors.js';
|
|
3
|
+
export type { AuditEvent, AuditEventFilter, AuditEventPage } from './types.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,EACV,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,eAAe,EACf,yBAAyB,EACzB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { ProjectId, TenantId, Timestamp } from '@kindgi/types';
|
|
2
|
+
/**
|
|
3
|
+
* One canonical event in the audit log. Every framework subsystem
|
|
4
|
+
* emits events of this shape — authz PDP decisions, secret set /
|
|
5
|
+
* rotation, run outcomes, guardrail violations, HITL decisions.
|
|
6
|
+
* Compliance is a classification lens over this stream.
|
|
7
|
+
*
|
|
8
|
+
* Design goals:
|
|
9
|
+
* - **One substrate**. No mirror wrappers, no dual writes. One table,
|
|
10
|
+
* one binding, one retention cleanup.
|
|
11
|
+
* - **Kind-agnostic core columns**. Common denormalized fields
|
|
12
|
+
* (`tenantId`, `projectId`, `timestamp`, `actor`, `correlationId`,
|
|
13
|
+
* `runId`) index-covered for the standard query patterns. Kind-
|
|
14
|
+
* specific data lives in `payload` — the source of truth for the
|
|
15
|
+
* on-wire event shape.
|
|
16
|
+
* - **Content-scope aware**. `projectId` is optional at emission time
|
|
17
|
+
* (some events legitimately transcend a single project — cross-
|
|
18
|
+
* tenant admin actions, for example — but most anchor to a project
|
|
19
|
+
* for query scoping).
|
|
20
|
+
*/
|
|
21
|
+
export interface AuditEvent {
|
|
22
|
+
/** Caller-supplied semantic id — must be unique per tenant. */
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly tenantId: TenantId;
|
|
25
|
+
/**
|
|
26
|
+
* Content-scope anchor. Optional so tenant-level events (e.g. admin
|
|
27
|
+
* actions taken before any project exists) can land without one.
|
|
28
|
+
* Events emitted inside a project carry it; queries use it for scope
|
|
29
|
+
* narrowing.
|
|
30
|
+
*/
|
|
31
|
+
readonly projectId?: ProjectId;
|
|
32
|
+
/**
|
|
33
|
+
* Event kind — the framework emits a known set (`authz-decision`,
|
|
34
|
+
* `secret-set`, `secret-rotated`, `run-outcome`, `guardrail-
|
|
35
|
+
* violation`, `hitl-decision`, …); open at the type level so pack
|
|
36
|
+
* authors can extend without touching the framework schema.
|
|
37
|
+
* The compliance classifier maps kinds → retention + signing +
|
|
38
|
+
* export classifications.
|
|
39
|
+
*/
|
|
40
|
+
readonly kind: string;
|
|
41
|
+
readonly timestamp: Timestamp;
|
|
42
|
+
/**
|
|
43
|
+
* Human/agent/service subject that caused this event.
|
|
44
|
+
* Convention: `<type>:<id>` — `user:u-1042`, `agent:demo.echo`,
|
|
45
|
+
* `service_account:migrator`, `user:system` for framework-internal
|
|
46
|
+
* actions. Present on every event.
|
|
47
|
+
*/
|
|
48
|
+
readonly actor: string;
|
|
49
|
+
/**
|
|
50
|
+
* Optional secondary subject — the party the actor acted on behalf
|
|
51
|
+
* of. Populated when a delegate chain matters (e.g. an agent
|
|
52
|
+
* running on-behalf-of a user).
|
|
53
|
+
*/
|
|
54
|
+
readonly onBehalfOf?: string;
|
|
55
|
+
readonly correlationId?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Kernel run id when this event was emitted inside a run. Absent
|
|
58
|
+
* for standalone events (secret rotations, background jobs).
|
|
59
|
+
*/
|
|
60
|
+
readonly runId?: string;
|
|
61
|
+
/** Agent id when this event was emitted from an agent turn. */
|
|
62
|
+
readonly agentId?: string;
|
|
63
|
+
/** Flow id when this event was emitted from a flow run. */
|
|
64
|
+
readonly flowId?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Outcome of the event when it has one; omitted for purely
|
|
67
|
+
* informational events. Free-form: `'allowed' | 'denied'` for authz
|
|
68
|
+
* decisions; `'succeeded' | 'failed'` for run outcomes and env /
|
|
69
|
+
* secret operations; etc.
|
|
70
|
+
*/
|
|
71
|
+
readonly outcome?: string;
|
|
72
|
+
/**
|
|
73
|
+
* The full event body — versioned envelope `{ v: 1, doc: ... }`
|
|
74
|
+
* so future schema drift migrates on read. Every kind has its own
|
|
75
|
+
* `doc` shape; the compliance classifier uses `kind` to know what
|
|
76
|
+
* to expect.
|
|
77
|
+
*/
|
|
78
|
+
readonly payload: Readonly<Record<string, unknown>>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Filter shape for reads. Every field is AND-combined; absent fields
|
|
82
|
+
* are wildcards. Cursor pagination uses `(timestamp asc, id asc)` as
|
|
83
|
+
* the keyset — same shape the storage adapter encodes as base64url.
|
|
84
|
+
*/
|
|
85
|
+
export interface AuditEventFilter {
|
|
86
|
+
/**
|
|
87
|
+
* Caller-supplied event id (matches `AuditEvent.id`). Combined with
|
|
88
|
+
* the query's `tenantId`, it matches at most one event (ids are
|
|
89
|
+
* unique per tenant) — the single-record lookup path.
|
|
90
|
+
*/
|
|
91
|
+
readonly id?: string;
|
|
92
|
+
readonly kind?: string;
|
|
93
|
+
/** Match any of these kinds. An empty array matches nothing (never "no filter"). */
|
|
94
|
+
readonly kinds?: readonly string[];
|
|
95
|
+
readonly actor?: string;
|
|
96
|
+
/** Matches `AuditEvent.onBehalfOf` exactly; events without one never match. */
|
|
97
|
+
readonly onBehalfOf?: string;
|
|
98
|
+
readonly runId?: string;
|
|
99
|
+
readonly agentId?: string;
|
|
100
|
+
readonly flowId?: string;
|
|
101
|
+
readonly correlationId?: string;
|
|
102
|
+
readonly outcome?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Match on fields of the versioned payload document: every entry
|
|
105
|
+
* must satisfy `payload.doc[key] === value` (string equality). Events
|
|
106
|
+
* whose payload has no `doc` object never match a non-empty filter.
|
|
107
|
+
* Lets callers filter on kind-specific fields (e.g. `action` /
|
|
108
|
+
* `resource` of an `authz-decision`) inside the query, so pages stay
|
|
109
|
+
* full and cursors stay exact.
|
|
110
|
+
*/
|
|
111
|
+
readonly payloadDoc?: Readonly<Record<string, string>>;
|
|
112
|
+
/** ISO date-time — inclusive lower bound on `timestamp`. */
|
|
113
|
+
readonly from?: string;
|
|
114
|
+
/** ISO date-time — inclusive upper bound on `timestamp`. */
|
|
115
|
+
readonly to?: string;
|
|
116
|
+
}
|
|
117
|
+
export interface AuditEventPage {
|
|
118
|
+
readonly data: readonly AuditEvent[];
|
|
119
|
+
readonly nextCursor?: string;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEpE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,UAAU;IACzB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B;;;;;;;OAOG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAC9B;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,2DAA2D;IAC3D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACrD;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,oFAAoF;IACpF,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,+EAA+E;IAC/E,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;OAOG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACvD,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,SAAS,UAAU,EAAE,CAAC;IACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kindgi/audit-events",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kindgi™ audit events contract. Types only: AuditEvent (the audit record — id, tenantId, projectId, kind, timestamp, actor, outcome, run / agent / flow / correlation ids, versioned payload), AuditEventFilter and AuditEventPage, AuditEventBinding (the caller-plugged append / query / purge interface) with its input and result shapes, and the AuditEventError union. No runtime code. A reference in-memory binding is @kindgi/audit-events-inmemory; durable bindings are provided by the Kindgi runtime.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"repository": {
|
|
7
|
-
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kindgi/kindgi-sdk.git",
|
|
9
|
+
"directory": "packages/audit-events"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kindgi/kindgi-sdk/tree/main/packages/audit-events#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kindgi/kindgi-sdk/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@kindgi/types": "0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.0.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"provenance": true
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.10.5",
|
|
41
|
+
"typescript": "^5.7.3",
|
|
42
|
+
"vitest": "^2.1.8"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.build.json",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"test": "vitest run --typecheck --typecheck.only --passWithNoTests",
|
|
48
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/binding.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { Result, TenantId } from '@kindgi/types';
|
|
5
|
+
|
|
6
|
+
import type { AuditEventValidationError, InvalidCursorError, PersistenceError } from './errors.js';
|
|
7
|
+
import type { AuditEvent, AuditEventFilter, AuditEventPage } from './types.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Caller-plugged, tenant-scoped write + query surface for the unified
|
|
11
|
+
* audit substrate.
|
|
12
|
+
*
|
|
13
|
+
* Contracts:
|
|
14
|
+
* - `append` — batched write; groups by tenant, one insert per
|
|
15
|
+
* tenant per call. Duplicates on `(tenantId, id)` are silent
|
|
16
|
+
* (idempotent — safer than throwing when the caller re-fires
|
|
17
|
+
* from a retry loop).
|
|
18
|
+
* - `query` — cursor-paginated read; opaque cursor tokens encode
|
|
19
|
+
* `(timestamp, id)` for keyset pagination.
|
|
20
|
+
* - `purge` — delete rows matching `(tenantId, kind, olderThan)`;
|
|
21
|
+
* retention cleanup calls this per kind. Legal-hold kinds are
|
|
22
|
+
* excluded by the caller (the compliance classifier decides).
|
|
23
|
+
*
|
|
24
|
+
* Signing lives at the export layer (compliance export), not on the
|
|
25
|
+
* write path. Row content is stable; signature is deterministic from
|
|
26
|
+
* canonical bytes.
|
|
27
|
+
*/
|
|
28
|
+
export interface AuditEventBinding {
|
|
29
|
+
append(
|
|
30
|
+
events: readonly AuditEvent[],
|
|
31
|
+
): Promise<Result<void, PersistenceError | AuditEventValidationError>>;
|
|
32
|
+
|
|
33
|
+
query(
|
|
34
|
+
input: AuditEventQueryInput,
|
|
35
|
+
): Promise<Result<AuditEventPage, PersistenceError | InvalidCursorError>>;
|
|
36
|
+
|
|
37
|
+
purge(input: AuditEventPurgeInput): Promise<Result<AuditEventPurgeResult, PersistenceError>>;
|
|
38
|
+
|
|
39
|
+
describe(): { readonly name: string; readonly version: string };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface AuditEventQueryInput {
|
|
43
|
+
readonly tenantId: TenantId;
|
|
44
|
+
readonly filter?: AuditEventFilter;
|
|
45
|
+
readonly cursor?: string;
|
|
46
|
+
readonly limit?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface AuditEventPurgeInput {
|
|
50
|
+
readonly tenantId: TenantId;
|
|
51
|
+
/**
|
|
52
|
+
* Event kind to purge. One purge = one kind — keeps retention
|
|
53
|
+
* cleanup's per-kind granularity explicit. Legal-hold kinds should
|
|
54
|
+
* never reach this call.
|
|
55
|
+
*/
|
|
56
|
+
readonly kind: string;
|
|
57
|
+
/** ISO timestamp cutoff. Rows with `timestamp < olderThan` are deleted. */
|
|
58
|
+
readonly olderThan: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface AuditEventPurgeResult {
|
|
62
|
+
readonly deleted: number;
|
|
63
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
export interface PersistenceError {
|
|
5
|
+
readonly code: 'persistence-error';
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly cause?: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface InvalidCursorError {
|
|
11
|
+
readonly code: 'invalid-cursor';
|
|
12
|
+
readonly message: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Rejected on `append` when an event fails schema validation before
|
|
17
|
+
* write. `issues` is the per-path breakdown from the underlying
|
|
18
|
+
* validator. Exported as `AuditEventValidationError` to disambiguate
|
|
19
|
+
* from `@kindgi/schema.ValidationError` (a different shape used for
|
|
20
|
+
* JSON schema validation failures at the type-registry boundary).
|
|
21
|
+
*/
|
|
22
|
+
export interface AuditEventValidationError {
|
|
23
|
+
readonly code: 'invalid-event';
|
|
24
|
+
readonly message: string;
|
|
25
|
+
readonly issues?: readonly { readonly path: string; readonly message: string }[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type AuditEventError = PersistenceError | InvalidCursorError | AuditEventValidationError;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
export type {
|
|
5
|
+
AuditEventBinding,
|
|
6
|
+
AuditEventPurgeInput,
|
|
7
|
+
AuditEventPurgeResult,
|
|
8
|
+
AuditEventQueryInput,
|
|
9
|
+
} from './binding.js';
|
|
10
|
+
export type {
|
|
11
|
+
AuditEventError,
|
|
12
|
+
AuditEventValidationError,
|
|
13
|
+
InvalidCursorError,
|
|
14
|
+
PersistenceError,
|
|
15
|
+
} from './errors.js';
|
|
16
|
+
export type { AuditEvent, AuditEventFilter, AuditEventPage } from './types.js';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { ProjectId, TenantId, Timestamp } from '@kindgi/types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One canonical event in the audit log. Every framework subsystem
|
|
8
|
+
* emits events of this shape — authz PDP decisions, secret set /
|
|
9
|
+
* rotation, run outcomes, guardrail violations, HITL decisions.
|
|
10
|
+
* Compliance is a classification lens over this stream.
|
|
11
|
+
*
|
|
12
|
+
* Design goals:
|
|
13
|
+
* - **One substrate**. No mirror wrappers, no dual writes. One table,
|
|
14
|
+
* one binding, one retention cleanup.
|
|
15
|
+
* - **Kind-agnostic core columns**. Common denormalized fields
|
|
16
|
+
* (`tenantId`, `projectId`, `timestamp`, `actor`, `correlationId`,
|
|
17
|
+
* `runId`) index-covered for the standard query patterns. Kind-
|
|
18
|
+
* specific data lives in `payload` — the source of truth for the
|
|
19
|
+
* on-wire event shape.
|
|
20
|
+
* - **Content-scope aware**. `projectId` is optional at emission time
|
|
21
|
+
* (some events legitimately transcend a single project — cross-
|
|
22
|
+
* tenant admin actions, for example — but most anchor to a project
|
|
23
|
+
* for query scoping).
|
|
24
|
+
*/
|
|
25
|
+
export interface AuditEvent {
|
|
26
|
+
/** Caller-supplied semantic id — must be unique per tenant. */
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly tenantId: TenantId;
|
|
29
|
+
/**
|
|
30
|
+
* Content-scope anchor. Optional so tenant-level events (e.g. admin
|
|
31
|
+
* actions taken before any project exists) can land without one.
|
|
32
|
+
* Events emitted inside a project carry it; queries use it for scope
|
|
33
|
+
* narrowing.
|
|
34
|
+
*/
|
|
35
|
+
readonly projectId?: ProjectId;
|
|
36
|
+
/**
|
|
37
|
+
* Event kind — the framework emits a known set (`authz-decision`,
|
|
38
|
+
* `secret-set`, `secret-rotated`, `run-outcome`, `guardrail-
|
|
39
|
+
* violation`, `hitl-decision`, …); open at the type level so pack
|
|
40
|
+
* authors can extend without touching the framework schema.
|
|
41
|
+
* The compliance classifier maps kinds → retention + signing +
|
|
42
|
+
* export classifications.
|
|
43
|
+
*/
|
|
44
|
+
readonly kind: string;
|
|
45
|
+
readonly timestamp: Timestamp;
|
|
46
|
+
/**
|
|
47
|
+
* Human/agent/service subject that caused this event.
|
|
48
|
+
* Convention: `<type>:<id>` — `user:u-1042`, `agent:demo.echo`,
|
|
49
|
+
* `service_account:migrator`, `user:system` for framework-internal
|
|
50
|
+
* actions. Present on every event.
|
|
51
|
+
*/
|
|
52
|
+
readonly actor: string;
|
|
53
|
+
/**
|
|
54
|
+
* Optional secondary subject — the party the actor acted on behalf
|
|
55
|
+
* of. Populated when a delegate chain matters (e.g. an agent
|
|
56
|
+
* running on-behalf-of a user).
|
|
57
|
+
*/
|
|
58
|
+
readonly onBehalfOf?: string;
|
|
59
|
+
readonly correlationId?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Kernel run id when this event was emitted inside a run. Absent
|
|
62
|
+
* for standalone events (secret rotations, background jobs).
|
|
63
|
+
*/
|
|
64
|
+
readonly runId?: string;
|
|
65
|
+
/** Agent id when this event was emitted from an agent turn. */
|
|
66
|
+
readonly agentId?: string;
|
|
67
|
+
/** Flow id when this event was emitted from a flow run. */
|
|
68
|
+
readonly flowId?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Outcome of the event when it has one; omitted for purely
|
|
71
|
+
* informational events. Free-form: `'allowed' | 'denied'` for authz
|
|
72
|
+
* decisions; `'succeeded' | 'failed'` for run outcomes and env /
|
|
73
|
+
* secret operations; etc.
|
|
74
|
+
*/
|
|
75
|
+
readonly outcome?: string;
|
|
76
|
+
/**
|
|
77
|
+
* The full event body — versioned envelope `{ v: 1, doc: ... }`
|
|
78
|
+
* so future schema drift migrates on read. Every kind has its own
|
|
79
|
+
* `doc` shape; the compliance classifier uses `kind` to know what
|
|
80
|
+
* to expect.
|
|
81
|
+
*/
|
|
82
|
+
readonly payload: Readonly<Record<string, unknown>>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Filter shape for reads. Every field is AND-combined; absent fields
|
|
87
|
+
* are wildcards. Cursor pagination uses `(timestamp asc, id asc)` as
|
|
88
|
+
* the keyset — same shape the storage adapter encodes as base64url.
|
|
89
|
+
*/
|
|
90
|
+
export interface AuditEventFilter {
|
|
91
|
+
/**
|
|
92
|
+
* Caller-supplied event id (matches `AuditEvent.id`). Combined with
|
|
93
|
+
* the query's `tenantId`, it matches at most one event (ids are
|
|
94
|
+
* unique per tenant) — the single-record lookup path.
|
|
95
|
+
*/
|
|
96
|
+
readonly id?: string;
|
|
97
|
+
readonly kind?: string;
|
|
98
|
+
/** Match any of these kinds. An empty array matches nothing (never "no filter"). */
|
|
99
|
+
readonly kinds?: readonly string[];
|
|
100
|
+
readonly actor?: string;
|
|
101
|
+
/** Matches `AuditEvent.onBehalfOf` exactly; events without one never match. */
|
|
102
|
+
readonly onBehalfOf?: string;
|
|
103
|
+
readonly runId?: string;
|
|
104
|
+
readonly agentId?: string;
|
|
105
|
+
readonly flowId?: string;
|
|
106
|
+
readonly correlationId?: string;
|
|
107
|
+
readonly outcome?: string;
|
|
108
|
+
/**
|
|
109
|
+
* Match on fields of the versioned payload document: every entry
|
|
110
|
+
* must satisfy `payload.doc[key] === value` (string equality). Events
|
|
111
|
+
* whose payload has no `doc` object never match a non-empty filter.
|
|
112
|
+
* Lets callers filter on kind-specific fields (e.g. `action` /
|
|
113
|
+
* `resource` of an `authz-decision`) inside the query, so pages stay
|
|
114
|
+
* full and cursors stay exact.
|
|
115
|
+
*/
|
|
116
|
+
readonly payloadDoc?: Readonly<Record<string, string>>;
|
|
117
|
+
/** ISO date-time — inclusive lower bound on `timestamp`. */
|
|
118
|
+
readonly from?: string;
|
|
119
|
+
/** ISO date-time — inclusive upper bound on `timestamp`. */
|
|
120
|
+
readonly to?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface AuditEventPage {
|
|
124
|
+
readonly data: readonly AuditEvent[];
|
|
125
|
+
readonly nextCursor?: string;
|
|
126
|
+
}
|