@parmanasystems/execution 1.0.19

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 ADDED
@@ -0,0 +1,314 @@
1
+ # @parmanasystems/execution
2
+
3
+ Deterministic runtime execution and governance attestation infrastructure for parmanasystems.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@parmanasystems/execution)](https://www.npmjs.com/package/@parmanasystems/execution)
6
+
7
+ ## Overview
8
+
9
+ `@parmanasystems/execution` is the core execution engine. It handles:
10
+
11
+ - **Execution tokens** — single-use authorization tokens binding a decision to specific inputs
12
+ - **Deterministic execution** — identical inputs always produce identical signed results
13
+ - **Replay protection** — each `execution_id` is consumed exactly once
14
+ - **Cryptographic attestation** — results are signed at execution time and independently verifiable
15
+ - **Invariant enforcement** — 60+ registered invariants with structured violation reports
16
+ - **Audit trail** — hash-chained JSONL log at `./runtime-audit/executions.jsonl`
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @parmanasystems/execution
22
+ ```
23
+
24
+ ## Quick start
25
+
26
+ ```typescript
27
+ import {
28
+ executeFromSignals,
29
+ LocalSigner,
30
+ LocalVerifier,
31
+ RedisReplayStore,
32
+ } from "@parmanasystems/execution";
33
+ import { createClient } from "redis";
34
+
35
+ const redis = createClient();
36
+ await redis.connect();
37
+
38
+ const privateKeyPem = process.env.Parmana_PRIVATE_KEY!;
39
+ const publicKeyPem = process.env.Parmana_PUBLIC_KEY!;
40
+
41
+ const signer = new LocalSigner(privateKeyPem);
42
+ const verifier = new LocalVerifier(publicKeyPem);
43
+ const store = new RedisReplayStore(redis);
44
+
45
+ const result = await executeFromSignals(
46
+ {
47
+ policyId: "claims-approval",
48
+ policyVersion: "v1",
49
+ signals: {
50
+ insurance_active: true,
51
+ risk_score: 42,
52
+ claim_amount: 50000,
53
+ },
54
+ },
55
+ signer,
56
+ verifier,
57
+ store
58
+ );
59
+
60
+ if (result.status === "success") {
61
+ console.log(result.execution_state); // "completed" | "blocked" | "pending_override"
62
+ console.log(result.signature); // base64 Ed25519 signature
63
+ }
64
+ ```
65
+
66
+ ## API
67
+
68
+ ### executeFromSignals
69
+
70
+ The primary high-level entry point. Loads the policy from `./policies/{policyId}/{policyVersion}/policy.json`, validates signals, evaluates rules, issues a token, and runs the full pipeline.
71
+
72
+ ```typescript
73
+ function executeFromSignals(
74
+ input: {
75
+ policyId: string;
76
+ policyVersion: string;
77
+ signals: Record<string, unknown>;
78
+ metadata?: Record<string, unknown>;
79
+ },
80
+ signer: Signer,
81
+ verifier: Verifier,
82
+ replayStore: AsyncReplayStore & { get?; set?; del? }
83
+ ): Promise<ExecuteFromSignalsResult>
84
+ ```
85
+
86
+ Returns `{ status: "success", execution_id, decision, execution_state, signature? }` or `{ status: "pending_override", execution_id, ... }` or `{ status: "error", error }`.
87
+
88
+ ### executeDecision
89
+
90
+ Lower-level synchronous execution. Takes an already-constructed `ExecutionContext` and runs verify → replay → execute → sign stages.
91
+
92
+ ```typescript
93
+ function executeDecision(
94
+ context: ExecutionContext,
95
+ replayStore: ReplayStore
96
+ ): ExecutionAttestation
97
+ ```
98
+
99
+ ### executeSimple
100
+
101
+ Convenience wrapper for the server route. Issues a token from `{ policyId, policyVersion, decisionType, signalsHash }` without loading a policy file.
102
+
103
+ ```typescript
104
+ function executeSimple(
105
+ input: { policyId: string; policyVersion: string; decisionType: string; signalsHash: string },
106
+ signer: Signer,
107
+ verifier: Verifier,
108
+ store?: ReplayStore
109
+ ): ExecutionAttestation
110
+ ```
111
+
112
+ ### resolveOverride
113
+
114
+ Resolves a `pending_override` execution stored in Redis.
115
+
116
+ ```typescript
117
+ function resolveOverride(
118
+ executionId: string,
119
+ signer: Signer,
120
+ replayStore: AsyncReplayStore & { get?; del? }
121
+ ): Promise<{ success: boolean; signature: string }>
122
+ ```
123
+
124
+ ### evaluatePolicy
125
+
126
+ Pure, deterministic policy rule evaluation.
127
+
128
+ ```typescript
129
+ function evaluatePolicy(
130
+ policy: PolicyDefinition,
131
+ signals: Record<string, unknown>
132
+ ): DecisionResult
133
+ ```
134
+
135
+ ### issueToken
136
+
137
+ ```typescript
138
+ function issueToken(input: {
139
+ execution_id: string;
140
+ policy_id: string;
141
+ decision_payload: DecisionOutcome;
142
+ schema_version: string;
143
+ runtime_version: string;
144
+ }): ExecutionToken
145
+ ```
146
+
147
+ ### getRuntimeManifest
148
+
149
+ ```typescript
150
+ function getRuntimeManifest(): RuntimeManifest
151
+ ```
152
+
153
+ ## Types
154
+
155
+ ### ExecutionAttestation
156
+
157
+ ```typescript
158
+ interface ExecutionAttestation {
159
+ execution_id: string;
160
+ decision: {
161
+ action: "approve" | "reject";
162
+ requires_override: boolean;
163
+ reason?: string;
164
+ };
165
+ execution_state: "completed" | "blocked" | "pending_override";
166
+ signature: string; // base64 Ed25519
167
+ runtime_hash: string; // SHA-256 of runtime manifest
168
+ }
169
+ ```
170
+
171
+ ### ExecutionToken
172
+
173
+ ```typescript
174
+ interface ExecutionToken {
175
+ execution_id: string;
176
+ policy_id: string;
177
+ decision_payload: DecisionOutcome;
178
+ schema_version: string;
179
+ runtime_version: string;
180
+ }
181
+ ```
182
+
183
+ ### ExecutionContext
184
+
185
+ ```typescript
186
+ interface ExecutionContext {
187
+ token: ExecutionToken;
188
+ token_signature: string;
189
+ signer: Signer;
190
+ verifier: Verifier;
191
+ runtime_manifest: RuntimeManifest;
192
+ runtime_requirements: RuntimeRequirements;
193
+ governed_time?: string; // ISO 8601 — injected deterministic timestamp
194
+ }
195
+ ```
196
+
197
+ ### RuntimeManifest
198
+
199
+ ```typescript
200
+ interface RuntimeManifest {
201
+ runtime_version: "1.0.0";
202
+ runtime_hash: string;
203
+ supported_schema_versions: readonly ["1.0.0"];
204
+ capabilities: readonly [
205
+ "deterministic-evaluation",
206
+ "attestation-signing",
207
+ "replay-protection",
208
+ "bundle-verification",
209
+ ];
210
+ }
211
+ ```
212
+
213
+ ### Signer / Verifier
214
+
215
+ ```typescript
216
+ interface Signer { sign(payload: string): string; }
217
+ interface Verifier { verify(payload: string, signature: string): boolean; }
218
+ ```
219
+
220
+ ### InvariantViolation
221
+
222
+ ```typescript
223
+ class InvariantViolation extends Error {
224
+ readonly report: ViolationReport;
225
+ }
226
+
227
+ interface ViolationReport {
228
+ invariant_id: string; // e.g., "INV-013"
229
+ boundary: string; // e.g., "replay"
230
+ reason: string;
231
+ input_hash: string; // SHA-256 of offending input
232
+ timestamp_seq: number; // monotonic counter
233
+ }
234
+ ```
235
+
236
+ ## Signers
237
+
238
+ ### LocalSigner
239
+
240
+ ```typescript
241
+ const signer = new LocalSigner(privateKeyPem);
242
+ // privateKeyPem: PEM-encoded Ed25519 private key
243
+ ```
244
+
245
+ ### LocalVerifier
246
+
247
+ ```typescript
248
+ const verifier = new LocalVerifier(publicKeyPem);
249
+ ```
250
+
251
+ ### AwsKmsSigner
252
+
253
+ ```typescript
254
+ import { AwsKmsSigner } from "@parmanasystems/execution";
255
+ const signer = new AwsKmsSigner({ keyId: "arn:aws:kms:...", region: "us-east-1" });
256
+ ```
257
+
258
+ ## Replay stores
259
+
260
+ ### MemoryReplayStore
261
+
262
+ In-process `Set`. Use for testing and single-process deployments.
263
+
264
+ ```typescript
265
+ const store = new MemoryReplayStore();
266
+ ```
267
+
268
+ ### RedisReplayStore
269
+
270
+ Uses Redis `SET NX` for atomic distributed replay protection.
271
+
272
+ ```typescript
273
+ const store = new RedisReplayStore(redisClient);
274
+ ```
275
+
276
+ ## Invariant registry
277
+
278
+ ```typescript
279
+ import { INVARIANT_REGISTRY, violate, InvariantViolation } from "@parmanasystems/execution";
280
+
281
+ // Access registry
282
+ console.log(INVARIANT_REGISTRY["INV-013"].description);
283
+
284
+ // Throw a violation
285
+ violate("INV-013", "replay", "execution_id already consumed", executionId);
286
+ ```
287
+
288
+ All new invariant IDs must be registered in `src/invariant-registry.ts`. The CI gate enforces this.
289
+
290
+ ## Audit
291
+
292
+ ```typescript
293
+ import { appendAuditRecord, verifyAudit } from "@parmanasystems/execution";
294
+
295
+ await appendAuditRecord(attestation); // appends to ./runtime-audit/executions.jsonl
296
+ await verifyAudit("./runtime-audit/executions.jsonl"); // verifies hash chain
297
+ ```
298
+
299
+ ## Pipeline stages
300
+
301
+ ```typescript
302
+ import {
303
+ stageCanonicalizeSignals,
304
+ stageValidateSignals,
305
+ stageVerify,
306
+ stageCheckReplay,
307
+ stageExecute,
308
+ stageSign,
309
+ } from "@parmanasystems/execution";
310
+ ```
311
+
312
+ ## License
313
+
314
+ Apache-2.0