@cendor/acttrace 0.2.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.
@@ -0,0 +1,145 @@
1
+ /**
2
+ * `@cendor/acttrace` — a tamper-evident, auto-populated audit log for AI decisions. The TS port of
3
+ * `cendor.acttrace` (regex/pattern detectors only; no Presidio).
4
+ *
5
+ * Construct an {@link AuditLog} and it **subscribes** to `@cendor/core`'s event stream: every
6
+ * instrumented model/tool call — and the context decisions `@cendor/contextkit` rides on the same
7
+ * stream — becomes an audit entry with no per-call wiring. You add only the explicit human-facing
8
+ * events (`decision`, `humanOversight`).
9
+ *
10
+ * Integrity comes from a **hash chain**, not a server: `entry.hash = sha256(prev_hash +
11
+ * canonical(entry))`, so editing any past entry breaks every entry after it. {@link verify} re-walks
12
+ * the chain offline. Byte-conformant with the Python implementation: the canonical bytes that are
13
+ * hashed (and the HMAC inputs) are identical across languages.
14
+ */
15
+ import { DETECTORS, type Detector, detectors, registerDetector } from './detectors.js';
16
+ import { PolicyViolation, guard } from './guard.js';
17
+ import { nerAvailable, nerRedactor } from './ner.js';
18
+ import { LOCALE_PACKS, enableEntropyDetector, enableLocalePack } from './packs.js';
19
+ import { Finding, Policy, redact, scan } from './policy.js';
20
+ import { type PyValue } from './pyjson.js';
21
+ import { type ChainStorage } from './storage.js';
22
+ export { DETECTORS, registerDetector, detectors, Policy, Finding, scan, redact, guard, PolicyViolation, enableLocalePack, enableEntropyDetector, LOCALE_PACKS, nerAvailable, nerRedactor, };
23
+ export type { Detector };
24
+ /** The `prev_hash` of the first entry: 64 ASCII zeros. */
25
+ export declare const GENESIS: string;
26
+ /**
27
+ * Warned when `AuditLog({ maxEntries })` is set without `path`. Bounding the in-memory ring relies on
28
+ * the file as the source of truth; without a path, evicted entries are lost entirely.
29
+ */
30
+ export declare class BoundedMemoryWithoutPathWarning extends Error {
31
+ constructor(message: string);
32
+ }
33
+ /** Frameworks with a bundled (starting-template) control mapping for {@link AuditLog.export}. */
34
+ export declare function frameworks(): string[];
35
+ /** `sha256(prev_hash + canonical({payload, seq, ts, type}))` — prev_hash TEXT-prepended, 4 keys. */
36
+ export declare function chainHash(prevHash: string, seq: PyValue, ts: PyValue, etype: PyValue, payload: PyValue): string;
37
+ /** HMAC over an export `_meta` header's four completeness fields (entries, head_hash, risk_tier, system). */
38
+ export declare function metaSignature(key: string | Uint8Array, meta: {
39
+ system?: PyValue;
40
+ risk_tier?: PyValue;
41
+ head_hash?: PyValue;
42
+ entries?: PyValue;
43
+ }): string;
44
+ /** One link in the hash chain. Field order == on-disk key order. */
45
+ export declare class AuditEntry {
46
+ readonly seq: number | bigint;
47
+ readonly ts: string;
48
+ readonly type: string;
49
+ readonly payload: PyValue;
50
+ readonly prev_hash: string;
51
+ readonly hash: string;
52
+ readonly sig: string;
53
+ constructor(seq: number | bigint, ts: string, type: string, payload: PyValue, prev_hash: string, hash: string, sig?: string);
54
+ }
55
+ export declare function defaultRedactor(obj: unknown): unknown;
56
+ export interface FlagOptions {
57
+ action?: string;
58
+ severity?: string;
59
+ data?: unknown;
60
+ /** Extra payload fields (Python `**fields`), e.g. `{ auto: true }`. */
61
+ extra?: Record<string, unknown>;
62
+ }
63
+ export interface AuditLogOptions {
64
+ riskTier?: string;
65
+ path?: string | null;
66
+ signingKey?: string | Uint8Array | null;
67
+ redact?: boolean;
68
+ redactor?: ((obj: unknown) => unknown) | null;
69
+ flagOnRedact?: boolean;
70
+ policy?: Policy | null;
71
+ maxEntries?: number | null;
72
+ /** Advanced: override the chain storage backend (defaults to fs when `path` is set, else memory). */
73
+ storage?: ChainStorage;
74
+ }
75
+ /** A hash-chained, append-only, auto-populating audit log. */
76
+ export declare class AuditLog {
77
+ readonly system: string;
78
+ readonly riskTier: string;
79
+ entries: AuditEntry[];
80
+ private readonly _signingKey;
81
+ private readonly _redact;
82
+ private readonly _policy;
83
+ private readonly _redactor;
84
+ private readonly _flagOnRedact;
85
+ private readonly _maxEntries;
86
+ private readonly _path;
87
+ private readonly _storage;
88
+ private _seq;
89
+ private _evictedFromMemory;
90
+ private _head;
91
+ constructor(system: string, opts?: AuditLogOptions);
92
+ /** The current chain head hash. Capture it to later assert completeness via `verify`. */
93
+ get head(): string;
94
+ /** Entries evicted from the in-memory ring by `maxEntries` (0 if unbounded). Never left the chain. */
95
+ get evictedFromMemory(): number;
96
+ private now;
97
+ /** @internal Append one chain link. Also invoked by {@link Decision}. */
98
+ _append(etype: string, payload: Record<string, unknown>): AuditEntry;
99
+ private readonly _onEvent;
100
+ /** Stop subscribing to the core event stream and close the log file handle (idempotent). */
101
+ detach(): void;
102
+ /**
103
+ * Group a unit of work. Auto-captured calls inside the async callback are tagged with this
104
+ * decision (via an `AsyncLocalStorage` scope). Returns the callback's result.
105
+ */
106
+ decision<T>(cb: (d: Decision) => T | Promise<T>, opts?: {
107
+ input?: unknown;
108
+ actor?: string;
109
+ }): Promise<T>;
110
+ /**
111
+ * Record a policy flag — a tamper-evident record that a data/usage policy fired. `action`/`severity`
112
+ * are normalized to lowercase; pass a category label in `data`, never the raw value. Auto-tags the
113
+ * active decision span.
114
+ */
115
+ flag(reason: string, opts?: FlagOptions): AuditEntry;
116
+ private entriesForExport;
117
+ private summary;
118
+ /** Write the chain as a JSONL evidence pack, optionally annotated with framework control IDs. */
119
+ export(path: string, framework?: string | null): void;
120
+ }
121
+ /** Handle for the active decision span (passed to the {@link AuditLog.decision} callback). */
122
+ export declare class Decision {
123
+ readonly log: AuditLog;
124
+ readonly id: string;
125
+ constructor(log: AuditLog, id: string);
126
+ /** Record decision metadata (e.g. `{ model, prompt_id }`). */
127
+ record(fields: Record<string, unknown>): void;
128
+ /** Record an Art. 14-style human-oversight event: who reviewed, what action, and a note. */
129
+ humanOversight(reviewer: string, action: string, note?: string): void;
130
+ /** Record a policy flag tagged to this decision. Returns the chained {@link AuditEntry}. */
131
+ flag(reason: string, opts?: FlagOptions): AuditEntry;
132
+ }
133
+ export interface VerifyOptions {
134
+ key?: string | Uint8Array | null;
135
+ expectedHead?: string | null;
136
+ expectEntries?: number | null;
137
+ }
138
+ /**
139
+ * Re-walk the hash chain in a JSONL file. Returns `[ok, detail]`. Detects edits and deletions,
140
+ * including tail-truncation. Never throws on a missing/corrupt file — returns `[false, detail]`.
141
+ */
142
+ export declare function verify(path: string, opts?: VerifyOptions): [boolean, string];
143
+ /** `acttrace verify <path> [--key K] [--expect-head H] [--expect-entries N]`. Returns an exit code. */
144
+ export declare function main(argv: string[]): number;
145
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,EACL,SAAS,EACT,KAAK,QAAQ,EACb,SAAS,EAET,gBAAgB,EAGjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAW,KAAK,OAAO,EAA4C,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,KAAK,YAAY,EAAmD,MAAM,cAAc,CAAC;AAElG,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,SAAS,EACT,MAAM,EACN,OAAO,EACP,IAAI,EACJ,MAAM,EACN,KAAK,EACL,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,YAAY,EACZ,YAAY,EACZ,WAAW,GACZ,CAAC;AACF,YAAY,EAAE,QAAQ,EAAE,CAAC;AAEzB,0DAA0D;AAC1D,eAAO,MAAM,OAAO,QAAiB,CAAC;AAUtC;;;GAGG;AACH,qBAAa,+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