atabey-shared 0.0.37 → 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/README.md +64 -25
- package/dist/agent-identity.d.ts +1 -0
- package/dist/agent-identity.js +2 -0
- package/dist/agent-identity.js.map +1 -0
- package/dist/api-catalog.d.ts +1 -0
- package/dist/api-catalog.js +2 -0
- package/dist/api-catalog.js.map +1 -0
- package/dist/audit-chain.d.ts +1 -51
- package/dist/audit-chain.js +1 -84
- package/dist/audit-chain.js.map +1 -1
- package/dist/audit.d.ts +1 -113
- package/dist/audit.js +1 -284
- package/dist/audit.js.map +1 -1
- package/dist/constants.d.ts +1 -152
- package/dist/constants.js +1 -215
- package/dist/constants.js.map +1 -1
- package/dist/database.d.ts +1 -5
- package/dist/database.js +1 -12
- package/dist/database.js.map +1 -1
- package/dist/errors.d.ts +1 -46
- package/dist/errors.js +1 -68
- package/dist/errors.js.map +1 -1
- package/dist/file-lock.d.ts +1 -88
- package/dist/file-lock.js +1 -250
- package/dist/file-lock.js.map +1 -1
- package/dist/fs.d.ts +1 -9
- package/dist/fs.js +1 -53
- package/dist/fs.js.map +1 -1
- package/dist/index.d.ts +2 -22
- package/dist/index.js +2 -22
- package/dist/index.js.map +1 -1
- package/dist/job-queue.d.ts +1 -53
- package/dist/job-queue.js +1 -126
- package/dist/job-queue.js.map +1 -1
- package/dist/lock.d.ts +1 -82
- package/dist/lock.js +1 -158
- package/dist/lock.js.map +1 -1
- package/dist/logger.d.ts +1 -30
- package/dist/logger.js +1 -126
- package/dist/logger.js.map +1 -1
- package/dist/memory-prune.d.ts +1 -0
- package/dist/memory-prune.js +2 -0
- package/dist/memory-prune.js.map +1 -0
- package/dist/migrations.d.ts +1 -0
- package/dist/migrations.js +2 -0
- package/dist/migrations.js.map +1 -0
- package/dist/modules/agent/agent-identity.d.ts +34 -0
- package/dist/modules/agent/agent-identity.js +77 -0
- package/dist/modules/agent/agent-identity.js.map +1 -0
- package/dist/modules/agent/api-catalog.d.ts +57 -0
- package/dist/modules/agent/api-catalog.js +254 -0
- package/dist/modules/agent/api-catalog.js.map +1 -0
- package/dist/modules/agent/index.d.ts +3 -0
- package/dist/modules/agent/index.js +4 -0
- package/dist/modules/agent/index.js.map +1 -0
- package/dist/modules/agent/tokenizer.d.ts +54 -0
- package/dist/modules/agent/tokenizer.js +272 -0
- package/dist/modules/agent/tokenizer.js.map +1 -0
- package/dist/modules/concurrency/file-lock.d.ts +93 -0
- package/dist/modules/concurrency/file-lock.js +240 -0
- package/dist/modules/concurrency/file-lock.js.map +1 -0
- package/dist/modules/concurrency/index.d.ts +6 -0
- package/dist/modules/concurrency/index.js +7 -0
- package/dist/modules/concurrency/index.js.map +1 -0
- package/dist/modules/concurrency/job-queue.d.ts +76 -0
- package/dist/modules/concurrency/job-queue.js +200 -0
- package/dist/modules/concurrency/job-queue.js.map +1 -0
- package/dist/modules/concurrency/lock.d.ts +82 -0
- package/dist/modules/concurrency/lock.js +159 -0
- package/dist/modules/concurrency/lock.js.map +1 -0
- package/dist/modules/concurrency/resilience.d.ts +84 -0
- package/dist/modules/concurrency/resilience.js +217 -0
- package/dist/modules/concurrency/resilience.js.map +1 -0
- package/dist/modules/concurrency/worker-pool.d.ts +52 -0
- package/dist/modules/concurrency/worker-pool.js +229 -0
- package/dist/modules/concurrency/worker-pool.js.map +1 -0
- package/dist/modules/concurrency/worker-thread.d.ts +21 -0
- package/dist/modules/concurrency/worker-thread.js +67 -0
- package/dist/modules/concurrency/worker-thread.js.map +1 -0
- package/dist/modules/index.d.ts +14 -0
- package/dist/modules/index.js +15 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/observability/audit-chain.d.ts +80 -0
- package/dist/modules/observability/audit-chain.js +262 -0
- package/dist/modules/observability/audit-chain.js.map +1 -0
- package/dist/modules/observability/audit.d.ts +121 -0
- package/dist/modules/observability/audit.js +322 -0
- package/dist/modules/observability/audit.js.map +1 -0
- package/dist/modules/observability/index.d.ts +4 -0
- package/dist/modules/observability/index.js +5 -0
- package/dist/modules/observability/index.js.map +1 -0
- package/dist/modules/observability/logger.d.ts +31 -0
- package/dist/modules/observability/logger.js +153 -0
- package/dist/modules/observability/logger.js.map +1 -0
- package/dist/modules/observability/otel.d.ts +39 -0
- package/dist/modules/observability/otel.js +93 -0
- package/dist/modules/observability/otel.js.map +1 -0
- package/dist/modules/primitives/constants.d.ts +150 -0
- package/dist/modules/primitives/constants.js +212 -0
- package/dist/modules/primitives/constants.js.map +1 -0
- package/dist/modules/primitives/errors.d.ts +46 -0
- package/dist/modules/primitives/errors.js +69 -0
- package/dist/modules/primitives/errors.js.map +1 -0
- package/dist/modules/primitives/fs.d.ts +9 -0
- package/dist/modules/primitives/fs.js +54 -0
- package/dist/modules/primitives/fs.js.map +1 -0
- package/dist/modules/primitives/index.d.ts +7 -0
- package/dist/modules/primitives/index.js +8 -0
- package/dist/modules/primitives/index.js.map +1 -0
- package/dist/modules/primitives/outbound-http.d.ts +32 -0
- package/dist/modules/primitives/outbound-http.js +88 -0
- package/dist/modules/primitives/outbound-http.js.map +1 -0
- package/dist/modules/primitives/string.d.ts +1 -0
- package/dist/modules/primitives/string.js +30 -0
- package/dist/modules/primitives/string.js.map +1 -0
- package/dist/modules/primitives/time.d.ts +5 -0
- package/dist/modules/primitives/time.js +35 -0
- package/dist/modules/primitives/time.js.map +1 -0
- package/dist/modules/primitives/types.d.ts +27 -0
- package/dist/modules/primitives/types.js +17 -0
- package/dist/modules/primitives/types.js.map +1 -0
- package/dist/modules/sandbox/index.d.ts +3 -0
- package/dist/modules/sandbox/index.js +4 -0
- package/dist/modules/sandbox/index.js.map +1 -0
- package/dist/modules/sandbox/sandbox-fs.d.ts +28 -0
- package/dist/modules/sandbox/sandbox-fs.js +143 -0
- package/dist/modules/sandbox/sandbox-fs.js.map +1 -0
- package/dist/modules/sandbox/sandbox-runtime.d.ts +78 -0
- package/dist/modules/sandbox/sandbox-runtime.js +282 -0
- package/dist/modules/sandbox/sandbox-runtime.js.map +1 -0
- package/dist/modules/sandbox/sandbox.d.ts +54 -0
- package/dist/modules/sandbox/sandbox.js +179 -0
- package/dist/modules/sandbox/sandbox.js.map +1 -0
- package/dist/modules/security/index.d.ts +5 -0
- package/dist/modules/security/index.js +6 -0
- package/dist/modules/security/index.js.map +1 -0
- package/dist/modules/security/pii.d.ts +70 -0
- package/dist/modules/security/pii.js +276 -0
- package/dist/modules/security/pii.js.map +1 -0
- package/dist/modules/security/policy-gate.d.ts +42 -0
- package/dist/modules/security/policy-gate.js +172 -0
- package/dist/modules/security/policy-gate.js.map +1 -0
- package/dist/modules/security/retention.d.ts +68 -0
- package/dist/modules/security/retention.js +218 -0
- package/dist/modules/security/retention.js.map +1 -0
- package/dist/modules/security/secrets.d.ts +99 -0
- package/dist/modules/security/secrets.js +371 -0
- package/dist/modules/security/secrets.js.map +1 -0
- package/dist/modules/security/tls.d.ts +87 -0
- package/dist/modules/security/tls.js +154 -0
- package/dist/modules/security/tls.js.map +1 -0
- package/dist/modules/storage/database.d.ts +6 -0
- package/dist/modules/storage/database.js +16 -0
- package/dist/modules/storage/database.js.map +1 -0
- package/dist/modules/storage/index.d.ts +4 -0
- package/dist/modules/storage/index.js +5 -0
- package/dist/modules/storage/index.js.map +1 -0
- package/dist/modules/storage/memory-prune.d.ts +18 -0
- package/dist/modules/storage/memory-prune.js +48 -0
- package/dist/modules/storage/memory-prune.js.map +1 -0
- package/dist/modules/storage/migrations.d.ts +69 -0
- package/dist/modules/storage/migrations.js +394 -0
- package/dist/modules/storage/migrations.js.map +1 -0
- package/dist/modules/storage/storage.d.ts +341 -0
- package/dist/modules/storage/storage.js +1004 -0
- package/dist/modules/storage/storage.js.map +1 -0
- package/dist/otel.d.ts +1 -0
- package/dist/otel.js +2 -0
- package/dist/otel.js.map +1 -0
- package/dist/outbound-http.d.ts +1 -0
- package/dist/outbound-http.js +2 -0
- package/dist/outbound-http.js.map +1 -0
- package/dist/pii.d.ts +1 -69
- package/dist/pii.js +1 -219
- package/dist/pii.js.map +1 -1
- package/dist/policy-gate.d.ts +1 -42
- package/dist/policy-gate.js +1 -147
- package/dist/policy-gate.js.map +1 -1
- package/dist/resilience.d.ts +1 -84
- package/dist/resilience.js +1 -216
- package/dist/resilience.js.map +1 -1
- package/dist/retention.d.ts +1 -63
- package/dist/retention.js +1 -170
- package/dist/retention.js.map +1 -1
- package/dist/sandbox-fs.d.ts +1 -28
- package/dist/sandbox-fs.js +1 -149
- package/dist/sandbox-fs.js.map +1 -1
- package/dist/sandbox-runtime.d.ts +1 -85
- package/dist/sandbox-runtime.js +1 -249
- package/dist/sandbox-runtime.js.map +1 -1
- package/dist/sandbox.d.ts +1 -54
- package/dist/sandbox.js +1 -178
- package/dist/sandbox.js.map +1 -1
- package/dist/secrets.d.ts +1 -0
- package/dist/secrets.js +2 -0
- package/dist/secrets.js.map +1 -0
- package/dist/storage.d.ts +1 -274
- package/dist/storage.js +1 -723
- package/dist/storage.js.map +1 -1
- package/dist/string.d.ts +1 -1
- package/dist/string.js +1 -29
- package/dist/string.js.map +1 -1
- package/dist/time.d.ts +1 -5
- package/dist/time.js +1 -30
- package/dist/time.js.map +1 -1
- package/dist/tls.d.ts +1 -0
- package/dist/tls.js +2 -0
- package/dist/tls.js.map +1 -0
- package/dist/tokenizer.d.ts +1 -0
- package/dist/tokenizer.js +2 -0
- package/dist/tokenizer.js.map +1 -0
- package/dist/types.d.ts +1 -27
- package/dist/types.js +1 -16
- package/dist/types.js.map +1 -1
- package/dist/worker-pool.d.ts +1 -52
- package/dist/worker-pool.js +1 -209
- package/dist/worker-pool.js.map +1 -1
- package/dist/worker-thread.d.ts +1 -21
- package/dist/worker-thread.js +1 -66
- package/dist/worker-thread.js.map +1 -1
- package/package.json +51 -29
package/README.md
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
# [GOV] Atabey Shared — Core Utility Library for AI Governance Platform
|
|
2
2
|
|
|
3
|
-
[](https://github.com/ysf-bkr/atabey)
|
|
4
4
|
[](https://www.npmjs.com/package/atabey-shared)
|
|
5
5
|
[](https://www.gnu.org/licenses/agpl-3.0)
|
|
6
6
|
|
|
7
|
-
**Atabey Shared** is the foundational utility package of the
|
|
7
|
+
**Atabey Shared** is the foundational utility package of the Atabey framework. It provides the shared codebase, data structures, constants, security filters, database holders, logging layers, and locking mechanisms used by both `atabey` (Core CLI/Orchestrator) and `atabey-mcp` (MCP Server).
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
## 📋 Package Overview
|
|
12
12
|
|
|
13
|
-
| Module | Files |
|
|
14
|
-
|
|
15
|
-
| **
|
|
16
|
-
| **
|
|
17
|
-
| **
|
|
18
|
-
| **
|
|
19
|
-
| **
|
|
20
|
-
| **
|
|
21
|
-
| **
|
|
22
|
-
| **File System** | `fs.ts` | Safe file write wrapper with automatic parent directory creation and encoding validation |
|
|
23
|
-
| **Database Holder** | `database.ts` | Singleton SQLite database holder for shared workspace storage |
|
|
13
|
+
| Module group | Files | Responsibility |
|
|
14
|
+
|--------------|-------|----------------|
|
|
15
|
+
| **primitives** | `constants`, `types`, `errors`, `fs`, `string`, `time`, `outbound-http` | Framework constants, branded types, error classes, safe fs, TLS-aware outbound HTTP |
|
|
16
|
+
| **storage** | `database`, `storage`, `migrations`, `memory-prune` | SQLite holder (singleton), schema migrations, memory pruning |
|
|
17
|
+
| **security** | `pii`, `secrets`, `tls`, `policy-gate`, `retention` | PII masking, AES-256-GCM vault, TLS/mTLS, fail-closed PolicyGate, retention |
|
|
18
|
+
| **concurrency** | `lock`, `file-lock`, `job-queue`, `worker-pool`, `worker-thread`, `resilience` | Distributed/file locks, DB-backed job queue, worker pool, circuit breaker |
|
|
19
|
+
| **sandbox** | `sandbox`, `sandbox-runtime`, `sandbox-fs` | OS sandbox (seatbelt/uid), fail-closed sandboxed file writes |
|
|
20
|
+
| **observability** | `logger`, `otel`, `audit`, `audit-chain` | Structured logging, OTel spans, HMAC-signed audit chain |
|
|
21
|
+
| **agent** | `agent-identity`, `api-catalog`, `tokenizer` | Signed agent identity, API endpoint catalog, `cl100k_base` tokenizer |
|
|
24
22
|
|
|
25
23
|
---
|
|
26
24
|
|
|
@@ -34,6 +32,44 @@ npm install atabey-shared
|
|
|
34
32
|
|
|
35
33
|
---
|
|
36
34
|
|
|
35
|
+
## 🧩 Module Structure
|
|
36
|
+
|
|
37
|
+
The package is organized into focused **module groups**. `src/index.ts` is a barrel
|
|
38
|
+
that re-exports everything flat **plus** a `Modules.<Group>` namespace per group;
|
|
39
|
+
the flat `atabey-shared/*.js` entry points are kept as thin re-export shims for
|
|
40
|
+
backward compatibility.
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
packages/shared/
|
|
44
|
+
├── src/
|
|
45
|
+
│ ├── index.ts # Barrel: flat exports + `Modules.<Group>`
|
|
46
|
+
│ └── modules/
|
|
47
|
+
│ ├── primitives/ # constants, types, errors, fs, string, time, outbound-http
|
|
48
|
+
│ ├── storage/ # database, storage, migrations, memory-prune
|
|
49
|
+
│ ├── security/ # pii, secrets, tls, policy-gate, retention
|
|
50
|
+
│ ├── concurrency/ # lock, file-lock, job-queue, worker-pool, worker-thread, resilience
|
|
51
|
+
│ ├── sandbox/ # sandbox, sandbox-runtime, sandbox-fs
|
|
52
|
+
│ ├── observability/ # logger, otel, audit, audit-chain
|
|
53
|
+
│ └── agent/ # agent-identity, api-catalog, tokenizer
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Three import styles are supported:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// 1. Flat — stable public API
|
|
60
|
+
import { maskText, logger } from "atabey-shared";
|
|
61
|
+
import { maskText } from "atabey-shared/pii.js";
|
|
62
|
+
|
|
63
|
+
// 2. Namespaced by module group
|
|
64
|
+
import { Modules } from "atabey-shared";
|
|
65
|
+
Modules.Security.maskText("user@example.com");
|
|
66
|
+
|
|
67
|
+
// 3. Deep module subpath
|
|
68
|
+
import { maskText } from "atabey-shared/security/pii.js";
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
37
73
|
## ⚙️ Core Modules Detail
|
|
38
74
|
|
|
39
75
|
### 1. PII Protection (KVKK/GDPR Compliance)
|
|
@@ -48,23 +84,26 @@ console.log(sanitizedText);
|
|
|
48
84
|
```
|
|
49
85
|
|
|
50
86
|
### 2. Distributed Resource Locking
|
|
51
|
-
The `lock.ts` module
|
|
87
|
+
The `lock.ts` module provides a SQLite-backed `DistributedLock` (server-authoritative,
|
|
88
|
+
with stale-lock reclaim) that prevents race conditions between parallel agents writing
|
|
89
|
+
to shared resources (e.g. `PROJECT_MEMORY.md`). For a lightweight file-based lock, use
|
|
90
|
+
`AtomicFileLock` from `file-lock.ts`.
|
|
52
91
|
```typescript
|
|
53
|
-
import {
|
|
92
|
+
import { DistributedLock } from "atabey-shared";
|
|
54
93
|
|
|
55
|
-
|
|
94
|
+
DistributedLock.initialize(); // once at startup
|
|
56
95
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
//
|
|
96
|
+
const owner = DistributedLock.getLocalOwnerId();
|
|
97
|
+
if (DistributedLock.acquire("PROJECT_MEMORY.md", owner, { ttlMs: 10000, reason: "memory sync" })) {
|
|
98
|
+
try {
|
|
99
|
+
// safe region: write the shared resource
|
|
100
|
+
} finally {
|
|
101
|
+
DistributedLock.release("PROJECT_MEMORY.md", owner);
|
|
61
102
|
}
|
|
62
|
-
} finally {
|
|
63
|
-
await lock.release();
|
|
64
103
|
}
|
|
65
104
|
```
|
|
66
105
|
|
|
67
|
-
### 3.
|
|
106
|
+
### 3. Structured Logger
|
|
68
107
|
Structured logger wrapping `process.stdout` and `process.stderr` supporting log file logging and JSON output format.
|
|
69
108
|
```typescript
|
|
70
109
|
import { logger, LogLevel } from "atabey-shared";
|
|
@@ -91,8 +130,8 @@ const task: TaskID = asTaskID("TASK-102");
|
|
|
91
130
|
|
|
92
131
|
## 📦 Dependencies
|
|
93
132
|
|
|
94
|
-
- **
|
|
95
|
-
- **
|
|
133
|
+
- **js-tiktoken**: `cl100k_base` BPE tokenizer for token/cost accounting
|
|
134
|
+
- **better-sqlite3**, **chalk**, **zod** _(peer)_: storage, terminal styling and runtime validation
|
|
96
135
|
|
|
97
136
|
---
|
|
98
137
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./modules/agent/agent-identity.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-identity.js","sourceRoot":"","sources":["../src/agent-identity.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./modules/agent/api-catalog.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-catalog.js","sourceRoot":"","sources":["../src/api-catalog.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC"}
|
package/dist/audit-chain.d.ts
CHANGED
|
@@ -1,51 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Tamper-evident audit hash chain helpers (Phase 1.4).
|
|
3
|
-
*
|
|
4
|
-
* Chain: each entry stores prev_hash (previous entry hash or GENESIS) and hash = SHA-256(payload).
|
|
5
|
-
* Any mutation of historical rows fails verification.
|
|
6
|
-
*/
|
|
7
|
-
export declare const AUDIT_CHAIN_GENESIS = "GENESIS";
|
|
8
|
-
export declare function sha256Hex(payload: string): string;
|
|
9
|
-
/**
|
|
10
|
-
* Canonical payload for agent execution logs (Storage.saveLog).
|
|
11
|
-
* MUST stay stable — changing it breaks verification of existing DBs.
|
|
12
|
-
*/
|
|
13
|
-
export declare function buildAgentLogChainPayload(fields: {
|
|
14
|
-
prevHash: string;
|
|
15
|
-
agent: string;
|
|
16
|
-
action: string;
|
|
17
|
-
traceId: string;
|
|
18
|
-
status: string;
|
|
19
|
-
summary: string;
|
|
20
|
-
timestamp: string;
|
|
21
|
-
}): string;
|
|
22
|
-
/**
|
|
23
|
-
* Canonical payload for structured Audit.log entries.
|
|
24
|
-
*/
|
|
25
|
-
export declare function buildStructuredAuditChainPayload(fields: {
|
|
26
|
-
prevHash: string;
|
|
27
|
-
agent: string;
|
|
28
|
-
action: string;
|
|
29
|
-
status: string;
|
|
30
|
-
traceId: string;
|
|
31
|
-
timestamp: string;
|
|
32
|
-
detailsJson: string;
|
|
33
|
-
errorMessage: string;
|
|
34
|
-
}): string;
|
|
35
|
-
export interface ChainRow {
|
|
36
|
-
id: number;
|
|
37
|
-
prev_hash: string | null;
|
|
38
|
-
hash: string | null;
|
|
39
|
-
}
|
|
40
|
-
export interface ChainVerifyResult {
|
|
41
|
-
valid: boolean;
|
|
42
|
-
checked: number;
|
|
43
|
-
failedLogId?: number;
|
|
44
|
-
reason?: string;
|
|
45
|
-
tip?: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Verify a sequence of rows already ordered by id ASC.
|
|
49
|
-
* `computePayload(row, prevHash)` must rebuild the exact string used at insert time.
|
|
50
|
-
*/
|
|
51
|
-
export declare function verifyHashChain<T extends ChainRow>(rows: T[], computePayload: (row: T, prevHash: string) => string): ChainVerifyResult;
|
|
1
|
+
export * from "./modules/observability/audit-chain.js";
|
package/dist/audit-chain.js
CHANGED
|
@@ -1,85 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Tamper-evident audit hash chain helpers (Phase 1.4).
|
|
3
|
-
*
|
|
4
|
-
* Chain: each entry stores prev_hash (previous entry hash or GENESIS) and hash = SHA-256(payload).
|
|
5
|
-
* Any mutation of historical rows fails verification.
|
|
6
|
-
*/
|
|
7
|
-
import crypto from "crypto";
|
|
8
|
-
export const AUDIT_CHAIN_GENESIS = "GENESIS";
|
|
9
|
-
export function sha256Hex(payload) {
|
|
10
|
-
return crypto.createHash("sha256").update(payload, "utf8").digest("hex");
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Canonical payload for agent execution logs (Storage.saveLog).
|
|
14
|
-
* MUST stay stable — changing it breaks verification of existing DBs.
|
|
15
|
-
*/
|
|
16
|
-
export function buildAgentLogChainPayload(fields) {
|
|
17
|
-
return [
|
|
18
|
-
fields.prevHash,
|
|
19
|
-
fields.agent,
|
|
20
|
-
fields.action,
|
|
21
|
-
fields.traceId,
|
|
22
|
-
fields.status,
|
|
23
|
-
fields.summary,
|
|
24
|
-
fields.timestamp,
|
|
25
|
-
].join("|");
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Canonical payload for structured Audit.log entries.
|
|
29
|
-
*/
|
|
30
|
-
export function buildStructuredAuditChainPayload(fields) {
|
|
31
|
-
return [
|
|
32
|
-
fields.prevHash,
|
|
33
|
-
fields.agent,
|
|
34
|
-
fields.action,
|
|
35
|
-
fields.status,
|
|
36
|
-
fields.traceId,
|
|
37
|
-
fields.timestamp,
|
|
38
|
-
fields.detailsJson,
|
|
39
|
-
fields.errorMessage,
|
|
40
|
-
].join("|");
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Verify a sequence of rows already ordered by id ASC.
|
|
44
|
-
* `computePayload(row, prevHash)` must rebuild the exact string used at insert time.
|
|
45
|
-
*/
|
|
46
|
-
export function verifyHashChain(rows, computePayload) {
|
|
47
|
-
let expectedPrev = rows.length > 0 ? (rows[0].prev_hash || AUDIT_CHAIN_GENESIS) : AUDIT_CHAIN_GENESIS;
|
|
48
|
-
let checked = 0;
|
|
49
|
-
for (const row of rows) {
|
|
50
|
-
checked++;
|
|
51
|
-
const prev = row.prev_hash || "";
|
|
52
|
-
if (prev !== expectedPrev) {
|
|
53
|
-
return {
|
|
54
|
-
valid: false,
|
|
55
|
-
checked,
|
|
56
|
-
failedLogId: row.id,
|
|
57
|
-
reason: `prev_hash mismatch at row ${row.id}`,
|
|
58
|
-
tip: "A log was deleted, reordered, or inserted out of chain order.",
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
if (!row.hash) {
|
|
62
|
-
return {
|
|
63
|
-
valid: false,
|
|
64
|
-
checked,
|
|
65
|
-
failedLogId: row.id,
|
|
66
|
-
reason: `Missing hash at row ${row.id}`,
|
|
67
|
-
tip: "Legacy row without hash; re-seed or accept and re-hash from tip.",
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
const payload = computePayload(row, prev);
|
|
71
|
-
const calculated = sha256Hex(payload);
|
|
72
|
-
if (calculated !== row.hash) {
|
|
73
|
-
return {
|
|
74
|
-
valid: false,
|
|
75
|
-
checked,
|
|
76
|
-
failedLogId: row.id,
|
|
77
|
-
reason: `Hash corruption at row ${row.id}`,
|
|
78
|
-
tip: "Row content was modified after append.",
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
expectedPrev = row.hash;
|
|
82
|
-
}
|
|
83
|
-
return { valid: true, checked };
|
|
84
|
-
}
|
|
1
|
+
export * from "./modules/observability/audit-chain.js";
|
|
85
2
|
//# sourceMappingURL=audit-chain.js.map
|
package/dist/audit-chain.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audit-chain.js","sourceRoot":"","sources":["../src/audit-chain.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"audit-chain.js","sourceRoot":"","sources":["../src/audit-chain.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC"}
|
package/dist/audit.d.ts
CHANGED
|
@@ -1,113 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* [AUDIT] Structured Audit Log Service
|
|
3
|
-
*
|
|
4
|
-
* Records all critical operations in structured JSON format to SQLite.
|
|
5
|
-
* Queryable, filterable, and traceable.
|
|
6
|
-
*
|
|
7
|
-
* [KVKK/GDPR] GDPR and KVKK Art. 4, 5, 11, 12 compliant:
|
|
8
|
-
* - Sensitive data automatically masked (PII masking)
|
|
9
|
-
* - Data retention limited (TTL)
|
|
10
|
-
* - Data subject rights supported (erasure, query)
|
|
11
|
-
*
|
|
12
|
-
* Usage:
|
|
13
|
-
* Audit.log("user.create", "SUCCESS", { userId: "usr_123" });
|
|
14
|
-
* Audit.query({ action: "user.create", status: "FAILED" });
|
|
15
|
-
*/
|
|
16
|
-
import { type ChainVerifyResult } from "./audit-chain.js";
|
|
17
|
-
export type AuditStatus = "SUCCESS" | "FAILED" | "BLOCKED" | "APPROVED" | "REJECTED";
|
|
18
|
-
export type DataCategory = "OPERATIONAL" | "USER_DATA" | "API_CALL" | "SECURITY" | "COMPLIANCE";
|
|
19
|
-
export interface AuditEntry {
|
|
20
|
-
id?: number;
|
|
21
|
-
timestamp: string;
|
|
22
|
-
action: string;
|
|
23
|
-
status: AuditStatus;
|
|
24
|
-
agent: string;
|
|
25
|
-
traceId?: string;
|
|
26
|
-
details?: Record<string, unknown>;
|
|
27
|
-
errorMessage?: string;
|
|
28
|
-
durationMs?: number;
|
|
29
|
-
dataCategory?: DataCategory;
|
|
30
|
-
/** [KVKK] Data retention period (days). null = unlimited (not recommended) */
|
|
31
|
-
retentionDays?: number;
|
|
32
|
-
/** Tamper-evident chain (Phase 1.4) */
|
|
33
|
-
prev_hash?: string;
|
|
34
|
-
hash?: string;
|
|
35
|
-
}
|
|
36
|
-
export declare class Audit {
|
|
37
|
-
private static TABLE;
|
|
38
|
-
/** [KVKK] Default data retention period: 30 days */
|
|
39
|
-
private static DEFAULT_RETENTION_DAYS;
|
|
40
|
-
/** [KVKK] Maximum retention for sensitive categories: 90 days */
|
|
41
|
-
private static MAX_RETENTION_DAYS;
|
|
42
|
-
static initialize(): void;
|
|
43
|
-
/**
|
|
44
|
-
* Logs a structured audit entry.
|
|
45
|
-
* [KVKK] Sensitive data is automatically masked.
|
|
46
|
-
*/
|
|
47
|
-
static log(action: string, status: AuditStatus, options?: {
|
|
48
|
-
agent?: string;
|
|
49
|
-
traceId?: string;
|
|
50
|
-
details?: Record<string, unknown>;
|
|
51
|
-
errorMessage?: string;
|
|
52
|
-
durationMs?: number;
|
|
53
|
-
dataCategory?: DataCategory;
|
|
54
|
-
/** [KVKK] Custom retention period (days). Default: 30 */
|
|
55
|
-
retentionDays?: number;
|
|
56
|
-
}): void;
|
|
57
|
-
/**
|
|
58
|
-
* Verify tamper-evident chain for structured audit_log table.
|
|
59
|
-
*/
|
|
60
|
-
static verifyIntegrity(): ChainVerifyResult;
|
|
61
|
-
/**
|
|
62
|
-
* Queries audit logs with filters.
|
|
63
|
-
*/
|
|
64
|
-
static query(options?: {
|
|
65
|
-
action?: string;
|
|
66
|
-
status?: AuditStatus;
|
|
67
|
-
agent?: string;
|
|
68
|
-
traceId?: string;
|
|
69
|
-
dataCategory?: DataCategory;
|
|
70
|
-
limit?: number;
|
|
71
|
-
offset?: number;
|
|
72
|
-
since?: string;
|
|
73
|
-
until?: string;
|
|
74
|
-
}): AuditEntry[];
|
|
75
|
-
/**
|
|
76
|
-
* [KVKK Art. 11] Deletes records upon data subject request.
|
|
77
|
-
* Clears all records matching a given traceId or agent.
|
|
78
|
-
*
|
|
79
|
-
* @param filter Which records to delete
|
|
80
|
-
* @returns Number of deleted records
|
|
81
|
-
*/
|
|
82
|
-
static deleteByFilter(filter: {
|
|
83
|
-
traceId?: string;
|
|
84
|
-
agent?: string;
|
|
85
|
-
action?: string;
|
|
86
|
-
olderThanDays?: number;
|
|
87
|
-
}): number;
|
|
88
|
-
/**
|
|
89
|
-
* [KVKK/GDPR] Cleans expired records.
|
|
90
|
-
* Operates based on each audit record's retentionDays field.
|
|
91
|
-
* Can be auto-triggered by cron job or each Audit.log() call.
|
|
92
|
-
*
|
|
93
|
-
* @returns Number of cleaned records
|
|
94
|
-
*/
|
|
95
|
-
static cleanExpired(): number;
|
|
96
|
-
/**
|
|
97
|
-
* Returns audit statistics.
|
|
98
|
-
*/
|
|
99
|
-
static getStats(): {
|
|
100
|
-
total: number;
|
|
101
|
-
byStatus: Record<string, number>;
|
|
102
|
-
byAction: Record<string, number>;
|
|
103
|
-
byCategory: Record<string, number>;
|
|
104
|
-
recentErrors: AuditEntry[];
|
|
105
|
-
expiredCount: number;
|
|
106
|
-
};
|
|
107
|
-
/**
|
|
108
|
-
* [KVKK Art. 7] Clears all audit records.
|
|
109
|
-
* Used under the "Right to Erasure" provision.
|
|
110
|
-
* Only authorized users may call this.
|
|
111
|
-
*/
|
|
112
|
-
static clearAll(confirmationCode: string): number;
|
|
113
|
-
}
|
|
1
|
+
export * from "./modules/observability/audit.js";
|