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.
Files changed (221) hide show
  1. package/README.md +64 -25
  2. package/dist/agent-identity.d.ts +1 -0
  3. package/dist/agent-identity.js +2 -0
  4. package/dist/agent-identity.js.map +1 -0
  5. package/dist/api-catalog.d.ts +1 -0
  6. package/dist/api-catalog.js +2 -0
  7. package/dist/api-catalog.js.map +1 -0
  8. package/dist/audit-chain.d.ts +1 -51
  9. package/dist/audit-chain.js +1 -84
  10. package/dist/audit-chain.js.map +1 -1
  11. package/dist/audit.d.ts +1 -113
  12. package/dist/audit.js +1 -284
  13. package/dist/audit.js.map +1 -1
  14. package/dist/constants.d.ts +1 -152
  15. package/dist/constants.js +1 -215
  16. package/dist/constants.js.map +1 -1
  17. package/dist/database.d.ts +1 -5
  18. package/dist/database.js +1 -12
  19. package/dist/database.js.map +1 -1
  20. package/dist/errors.d.ts +1 -46
  21. package/dist/errors.js +1 -68
  22. package/dist/errors.js.map +1 -1
  23. package/dist/file-lock.d.ts +1 -88
  24. package/dist/file-lock.js +1 -250
  25. package/dist/file-lock.js.map +1 -1
  26. package/dist/fs.d.ts +1 -9
  27. package/dist/fs.js +1 -53
  28. package/dist/fs.js.map +1 -1
  29. package/dist/index.d.ts +2 -22
  30. package/dist/index.js +2 -22
  31. package/dist/index.js.map +1 -1
  32. package/dist/job-queue.d.ts +1 -53
  33. package/dist/job-queue.js +1 -126
  34. package/dist/job-queue.js.map +1 -1
  35. package/dist/lock.d.ts +1 -82
  36. package/dist/lock.js +1 -158
  37. package/dist/lock.js.map +1 -1
  38. package/dist/logger.d.ts +1 -30
  39. package/dist/logger.js +1 -126
  40. package/dist/logger.js.map +1 -1
  41. package/dist/memory-prune.d.ts +1 -0
  42. package/dist/memory-prune.js +2 -0
  43. package/dist/memory-prune.js.map +1 -0
  44. package/dist/migrations.d.ts +1 -0
  45. package/dist/migrations.js +2 -0
  46. package/dist/migrations.js.map +1 -0
  47. package/dist/modules/agent/agent-identity.d.ts +34 -0
  48. package/dist/modules/agent/agent-identity.js +77 -0
  49. package/dist/modules/agent/agent-identity.js.map +1 -0
  50. package/dist/modules/agent/api-catalog.d.ts +57 -0
  51. package/dist/modules/agent/api-catalog.js +254 -0
  52. package/dist/modules/agent/api-catalog.js.map +1 -0
  53. package/dist/modules/agent/index.d.ts +3 -0
  54. package/dist/modules/agent/index.js +4 -0
  55. package/dist/modules/agent/index.js.map +1 -0
  56. package/dist/modules/agent/tokenizer.d.ts +54 -0
  57. package/dist/modules/agent/tokenizer.js +272 -0
  58. package/dist/modules/agent/tokenizer.js.map +1 -0
  59. package/dist/modules/concurrency/file-lock.d.ts +93 -0
  60. package/dist/modules/concurrency/file-lock.js +240 -0
  61. package/dist/modules/concurrency/file-lock.js.map +1 -0
  62. package/dist/modules/concurrency/index.d.ts +6 -0
  63. package/dist/modules/concurrency/index.js +7 -0
  64. package/dist/modules/concurrency/index.js.map +1 -0
  65. package/dist/modules/concurrency/job-queue.d.ts +76 -0
  66. package/dist/modules/concurrency/job-queue.js +200 -0
  67. package/dist/modules/concurrency/job-queue.js.map +1 -0
  68. package/dist/modules/concurrency/lock.d.ts +82 -0
  69. package/dist/modules/concurrency/lock.js +159 -0
  70. package/dist/modules/concurrency/lock.js.map +1 -0
  71. package/dist/modules/concurrency/resilience.d.ts +84 -0
  72. package/dist/modules/concurrency/resilience.js +217 -0
  73. package/dist/modules/concurrency/resilience.js.map +1 -0
  74. package/dist/modules/concurrency/worker-pool.d.ts +52 -0
  75. package/dist/modules/concurrency/worker-pool.js +229 -0
  76. package/dist/modules/concurrency/worker-pool.js.map +1 -0
  77. package/dist/modules/concurrency/worker-thread.d.ts +21 -0
  78. package/dist/modules/concurrency/worker-thread.js +67 -0
  79. package/dist/modules/concurrency/worker-thread.js.map +1 -0
  80. package/dist/modules/index.d.ts +14 -0
  81. package/dist/modules/index.js +15 -0
  82. package/dist/modules/index.js.map +1 -0
  83. package/dist/modules/observability/audit-chain.d.ts +80 -0
  84. package/dist/modules/observability/audit-chain.js +262 -0
  85. package/dist/modules/observability/audit-chain.js.map +1 -0
  86. package/dist/modules/observability/audit.d.ts +121 -0
  87. package/dist/modules/observability/audit.js +322 -0
  88. package/dist/modules/observability/audit.js.map +1 -0
  89. package/dist/modules/observability/index.d.ts +4 -0
  90. package/dist/modules/observability/index.js +5 -0
  91. package/dist/modules/observability/index.js.map +1 -0
  92. package/dist/modules/observability/logger.d.ts +31 -0
  93. package/dist/modules/observability/logger.js +153 -0
  94. package/dist/modules/observability/logger.js.map +1 -0
  95. package/dist/modules/observability/otel.d.ts +39 -0
  96. package/dist/modules/observability/otel.js +93 -0
  97. package/dist/modules/observability/otel.js.map +1 -0
  98. package/dist/modules/primitives/constants.d.ts +150 -0
  99. package/dist/modules/primitives/constants.js +212 -0
  100. package/dist/modules/primitives/constants.js.map +1 -0
  101. package/dist/modules/primitives/errors.d.ts +46 -0
  102. package/dist/modules/primitives/errors.js +69 -0
  103. package/dist/modules/primitives/errors.js.map +1 -0
  104. package/dist/modules/primitives/fs.d.ts +9 -0
  105. package/dist/modules/primitives/fs.js +54 -0
  106. package/dist/modules/primitives/fs.js.map +1 -0
  107. package/dist/modules/primitives/index.d.ts +7 -0
  108. package/dist/modules/primitives/index.js +8 -0
  109. package/dist/modules/primitives/index.js.map +1 -0
  110. package/dist/modules/primitives/outbound-http.d.ts +32 -0
  111. package/dist/modules/primitives/outbound-http.js +88 -0
  112. package/dist/modules/primitives/outbound-http.js.map +1 -0
  113. package/dist/modules/primitives/string.d.ts +1 -0
  114. package/dist/modules/primitives/string.js +30 -0
  115. package/dist/modules/primitives/string.js.map +1 -0
  116. package/dist/modules/primitives/time.d.ts +5 -0
  117. package/dist/modules/primitives/time.js +35 -0
  118. package/dist/modules/primitives/time.js.map +1 -0
  119. package/dist/modules/primitives/types.d.ts +27 -0
  120. package/dist/modules/primitives/types.js +17 -0
  121. package/dist/modules/primitives/types.js.map +1 -0
  122. package/dist/modules/sandbox/index.d.ts +3 -0
  123. package/dist/modules/sandbox/index.js +4 -0
  124. package/dist/modules/sandbox/index.js.map +1 -0
  125. package/dist/modules/sandbox/sandbox-fs.d.ts +28 -0
  126. package/dist/modules/sandbox/sandbox-fs.js +143 -0
  127. package/dist/modules/sandbox/sandbox-fs.js.map +1 -0
  128. package/dist/modules/sandbox/sandbox-runtime.d.ts +78 -0
  129. package/dist/modules/sandbox/sandbox-runtime.js +282 -0
  130. package/dist/modules/sandbox/sandbox-runtime.js.map +1 -0
  131. package/dist/modules/sandbox/sandbox.d.ts +54 -0
  132. package/dist/modules/sandbox/sandbox.js +179 -0
  133. package/dist/modules/sandbox/sandbox.js.map +1 -0
  134. package/dist/modules/security/index.d.ts +5 -0
  135. package/dist/modules/security/index.js +6 -0
  136. package/dist/modules/security/index.js.map +1 -0
  137. package/dist/modules/security/pii.d.ts +70 -0
  138. package/dist/modules/security/pii.js +276 -0
  139. package/dist/modules/security/pii.js.map +1 -0
  140. package/dist/modules/security/policy-gate.d.ts +42 -0
  141. package/dist/modules/security/policy-gate.js +172 -0
  142. package/dist/modules/security/policy-gate.js.map +1 -0
  143. package/dist/modules/security/retention.d.ts +68 -0
  144. package/dist/modules/security/retention.js +218 -0
  145. package/dist/modules/security/retention.js.map +1 -0
  146. package/dist/modules/security/secrets.d.ts +99 -0
  147. package/dist/modules/security/secrets.js +371 -0
  148. package/dist/modules/security/secrets.js.map +1 -0
  149. package/dist/modules/security/tls.d.ts +87 -0
  150. package/dist/modules/security/tls.js +154 -0
  151. package/dist/modules/security/tls.js.map +1 -0
  152. package/dist/modules/storage/database.d.ts +6 -0
  153. package/dist/modules/storage/database.js +16 -0
  154. package/dist/modules/storage/database.js.map +1 -0
  155. package/dist/modules/storage/index.d.ts +4 -0
  156. package/dist/modules/storage/index.js +5 -0
  157. package/dist/modules/storage/index.js.map +1 -0
  158. package/dist/modules/storage/memory-prune.d.ts +18 -0
  159. package/dist/modules/storage/memory-prune.js +48 -0
  160. package/dist/modules/storage/memory-prune.js.map +1 -0
  161. package/dist/modules/storage/migrations.d.ts +69 -0
  162. package/dist/modules/storage/migrations.js +394 -0
  163. package/dist/modules/storage/migrations.js.map +1 -0
  164. package/dist/modules/storage/storage.d.ts +341 -0
  165. package/dist/modules/storage/storage.js +1004 -0
  166. package/dist/modules/storage/storage.js.map +1 -0
  167. package/dist/otel.d.ts +1 -0
  168. package/dist/otel.js +2 -0
  169. package/dist/otel.js.map +1 -0
  170. package/dist/outbound-http.d.ts +1 -0
  171. package/dist/outbound-http.js +2 -0
  172. package/dist/outbound-http.js.map +1 -0
  173. package/dist/pii.d.ts +1 -69
  174. package/dist/pii.js +1 -219
  175. package/dist/pii.js.map +1 -1
  176. package/dist/policy-gate.d.ts +1 -42
  177. package/dist/policy-gate.js +1 -147
  178. package/dist/policy-gate.js.map +1 -1
  179. package/dist/resilience.d.ts +1 -84
  180. package/dist/resilience.js +1 -216
  181. package/dist/resilience.js.map +1 -1
  182. package/dist/retention.d.ts +1 -63
  183. package/dist/retention.js +1 -170
  184. package/dist/retention.js.map +1 -1
  185. package/dist/sandbox-fs.d.ts +1 -28
  186. package/dist/sandbox-fs.js +1 -149
  187. package/dist/sandbox-fs.js.map +1 -1
  188. package/dist/sandbox-runtime.d.ts +1 -85
  189. package/dist/sandbox-runtime.js +1 -249
  190. package/dist/sandbox-runtime.js.map +1 -1
  191. package/dist/sandbox.d.ts +1 -54
  192. package/dist/sandbox.js +1 -178
  193. package/dist/sandbox.js.map +1 -1
  194. package/dist/secrets.d.ts +1 -0
  195. package/dist/secrets.js +2 -0
  196. package/dist/secrets.js.map +1 -0
  197. package/dist/storage.d.ts +1 -274
  198. package/dist/storage.js +1 -723
  199. package/dist/storage.js.map +1 -1
  200. package/dist/string.d.ts +1 -1
  201. package/dist/string.js +1 -29
  202. package/dist/string.js.map +1 -1
  203. package/dist/time.d.ts +1 -5
  204. package/dist/time.js +1 -30
  205. package/dist/time.js.map +1 -1
  206. package/dist/tls.d.ts +1 -0
  207. package/dist/tls.js +2 -0
  208. package/dist/tls.js.map +1 -0
  209. package/dist/tokenizer.d.ts +1 -0
  210. package/dist/tokenizer.js +2 -0
  211. package/dist/tokenizer.js.map +1 -0
  212. package/dist/types.d.ts +1 -27
  213. package/dist/types.js +1 -16
  214. package/dist/types.js.map +1 -1
  215. package/dist/worker-pool.d.ts +1 -52
  216. package/dist/worker-pool.js +1 -209
  217. package/dist/worker-pool.js.map +1 -1
  218. package/dist/worker-thread.d.ts +1 -21
  219. package/dist/worker-thread.js +1 -66
  220. package/dist/worker-thread.js.map +1 -1
  221. 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
- [![Version](https://img.shields.io/badge/Version-v0.0.23-blue.svg)](https://github.com/ysf-bkr/atabey)
3
+ [![Version](https://img.shields.io/badge/Version-v0.1.0-blue.svg)](https://github.com/ysf-bkr/atabey)
4
4
  [![npm](https://img.shields.io/npm/v/atabey-shared)](https://www.npmjs.com/package/atabey-shared)
5
5
  [![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL--3.0-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
6
6
 
7
- **Atabey Shared** is the foundational utility package of the Agent 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).
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 | Core Responsibilities |
14
- |--------|-------|-----------------------|
15
- | **PII Masking** | `pii.ts` | Regex-based sanitization of 20+ sensitive patterns (emails, phones, credit cards, IP addresses, JWT tokens, API keys) |
16
- | **Distributed Locking** | `lock.ts` | Multi-process file-based lock implementation with stale lock detection (10s TTL) and retry backoff |
17
- | **Enterprise Logger** | `logger.ts` | Multi-destination structured logging (Stdout/Stderr + local files) supporting min-level and JSON formatting |
18
- | **Data Retention** | `retention.ts` | KVKK/GDPR-compliant telemetry and log pruning based on 30-day and 90-day retention policies |
19
- | **Audit Logs** | `audit.ts` | Unified JSON audit trail capturing agent actions, timestamps, trace IDs, and results |
20
- | **Error Handling** | `errors.ts` | Custom error classes (`AtabeyBaseError`, `LockError`, `ComplianceError`) preserving stack traces |
21
- | **Constants & Types** | `constants.ts`, `types.ts` | Centralized constants (Phase paths, candidate directories, MCP constants) and strictly branded types |
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 prevents race conditions between parallel running agents writing to shared resources (e.g. `PROJECT_MEMORY.md`).
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 { AtabeyLock } from "atabey-shared";
92
+ import { DistributedLock } from "atabey-shared";
54
93
 
55
- const lock = new AtabeyLock("project-memory", { ttlMs: 10000, retryCount: 3 });
94
+ DistributedLock.initialize(); // once at startup
56
95
 
57
- try {
58
- const acquired = await lock.acquire();
59
- if (acquired) {
60
- // Safe region: write to file
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. Enterprise Logger
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
- - **better-sqlite3**: SQLite database engine for storage shims
95
- - **typescript**: Type safety enforcement
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,2 @@
1
+ export * from "./modules/agent/agent-identity.js";
2
+ //# sourceMappingURL=agent-identity.js.map
@@ -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,2 @@
1
+ export * from "./modules/agent/api-catalog.js";
2
+ //# sourceMappingURL=api-catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-catalog.js","sourceRoot":"","sources":["../src/api-catalog.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC"}
@@ -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";
@@ -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
@@ -1 +1 @@
1
- {"version":3,"file":"audit-chain.js","sourceRoot":"","sources":["../src/audit-chain.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAE7C,MAAM,UAAU,SAAS,CAAC,OAAe;IACrC,OAAO,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAC,MAQzC;IACG,OAAO;QACH,MAAM,CAAC,QAAQ;QACf,MAAM,CAAC,KAAK;QACZ,MAAM,CAAC,MAAM;QACb,MAAM,CAAC,OAAO;QACd,MAAM,CAAC,MAAM;QACb,MAAM,CAAC,OAAO;QACd,MAAM,CAAC,SAAS;KACnB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gCAAgC,CAAC,MAShD;IACG,OAAO;QACH,MAAM,CAAC,QAAQ;QACf,MAAM,CAAC,KAAK;QACZ,MAAM,CAAC,MAAM;QACb,MAAM,CAAC,MAAM;QACb,MAAM,CAAC,OAAO;QACd,MAAM,CAAC,SAAS;QAChB,MAAM,CAAC,WAAW;QAClB,MAAM,CAAC,YAAY;KACtB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChB,CAAC;AAgBD;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC3B,IAAS,EACT,cAAoD;IAEpD,IAAI,YAAY,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,mBAAmB,CAAC,CAAC,CAAC,CAAC,mBAAmB,CAAC;IACtG,IAAI,OAAO,GAAG,CAAC,CAAC;IAEhB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC;QACV,MAAM,IAAI,GAAG,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC;QACjC,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;YACxB,OAAO;gBACH,KAAK,EAAE,KAAK;gBACZ,OAAO;gBACP,WAAW,EAAE,GAAG,CAAC,EAAE;gBACnB,MAAM,EAAE,6BAA6B,GAAG,CAAC,EAAE,EAAE;gBAC7C,GAAG,EAAE,+DAA+D;aACvE,CAAC;QACN,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;YACZ,OAAO;gBACH,KAAK,EAAE,KAAK;gBACZ,OAAO;gBACP,WAAW,EAAE,GAAG,CAAC,EAAE;gBACnB,MAAM,EAAE,uBAAuB,GAAG,CAAC,EAAE,EAAE;gBACvC,GAAG,EAAE,kEAAkE;aAC1E,CAAC;QACN,CAAC;QACD,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC1C,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;QACtC,IAAI,UAAU,KAAK,GAAG,CAAC,IAAI,EAAE,CAAC;YAC1B,OAAO;gBACH,KAAK,EAAE,KAAK;gBACZ,OAAO;gBACP,WAAW,EAAE,GAAG,CAAC,EAAE;gBACnB,MAAM,EAAE,0BAA0B,GAAG,CAAC,EAAE,EAAE;gBAC1C,GAAG,EAAE,wCAAwC;aAChD,CAAC;QACN,CAAC;QACD,YAAY,GAAG,GAAG,CAAC,IAAI,CAAC;IAC5B,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACpC,CAAC"}
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";