@sonnevhq/gate 0.0.0-stage → 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/dist/errors.js ADDED
@@ -0,0 +1,70 @@
1
+ export class GateError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "GateError";
5
+ Object.setPrototypeOf(this, new.target.prototype);
6
+ }
7
+ }
8
+ /**
9
+ * Thrown when the SONNEV Gate API cannot be reached.
10
+ * Enforcement fails closed: tool is NEVER executed.
11
+ */
12
+ export class GateUnreachableError extends GateError {
13
+ constructor(message = "SONNEV Gate is unreachable. Failing closed to prevent unauthorized tool execution.") {
14
+ super(message);
15
+ this.name = "GateUnreachableError";
16
+ }
17
+ }
18
+ /**
19
+ * Thrown when an action is denied by deterministic policy evaluation.
20
+ */
21
+ export class PolicyDeniedError extends GateError {
22
+ actionId;
23
+ reason;
24
+ matchedRule;
25
+ constructor(reason, actionId, matchedRule) {
26
+ super(`Action blocked by policy: ${reason}`);
27
+ this.name = "PolicyDeniedError";
28
+ this.actionId = actionId;
29
+ this.reason = reason;
30
+ this.matchedRule = matchedRule;
31
+ }
32
+ }
33
+ /**
34
+ * Thrown when an approval request times out without human authorization.
35
+ */
36
+ export class ApprovalTimeoutError extends GateError {
37
+ actionId;
38
+ constructor(message, actionId) {
39
+ super(message);
40
+ this.name = "ApprovalTimeoutError";
41
+ this.actionId = actionId;
42
+ }
43
+ }
44
+ /**
45
+ * Thrown when a human approver explicitly rejects the requested action.
46
+ */
47
+ export class ApprovalRejectedError extends GateError {
48
+ actionId;
49
+ reason;
50
+ constructor(actionId, reason) {
51
+ super(`Action rejected by approver: ${reason || "No rejection reason provided"}`);
52
+ this.name = "ApprovalRejectedError";
53
+ this.actionId = actionId;
54
+ this.reason = reason;
55
+ }
56
+ }
57
+ /**
58
+ * Thrown when tool post-execution verification fails intent checking.
59
+ */
60
+ export class VerificationFailedError extends GateError {
61
+ actionId;
62
+ reason;
63
+ constructor(reason, actionId) {
64
+ super(`Tool output failed post-execution verification: ${reason}`);
65
+ this.name = "VerificationFailedError";
66
+ this.actionId = actionId;
67
+ this.reason = reason;
68
+ }
69
+ }
70
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAqB,SAAQ,SAAS;IACjD,YAAY,UAAkB,oFAAoF;QAChH,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC9B,QAAQ,CAAU;IAClB,MAAM,CAAS;IACf,WAAW,CAKzB;IAEF,YACE,MAAc,EACd,QAAiB,EACjB,WAA4E;QAE5E,KAAK,CAAC,6BAA6B,MAAM,EAAE,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,SAAS;IACjC,QAAQ,CAAS;IAEjC,YAAY,OAAe,EAAE,QAAgB;QAC3C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,SAAS;IAClC,QAAQ,CAAS;IACjB,MAAM,CAAU;IAEhC,YAAY,QAAgB,EAAE,MAAe;QAC3C,KAAK,CAAC,gCAAgC,MAAM,IAAI,8BAA8B,EAAE,CAAC,CAAC;QAClF,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,SAAS;IACpC,QAAQ,CAAU;IAClB,MAAM,CAAS;IAE/B,YAAY,MAAc,EAAE,QAAiB;QAC3C,KAAK,CAAC,mDAAmD,MAAM,EAAE,CAAC,CAAC;QACnE,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF"}
package/dist/gate.d.ts ADDED
@@ -0,0 +1,138 @@
1
+ import { z } from "zod";
2
+ import { type SignedReceipt, type ReceiptVerificationResult } from "@sonnevhq/core";
3
+ import type { ActionStatusResponse, CheckActionResponse, GateConfig, WrapOptions } from "./types.js";
4
+ export declare const CheckActionResponseSchema: z.ZodObject<{
5
+ id: z.ZodString;
6
+ decision: z.ZodEnum<["allow", "deny", "pending_approval"]>;
7
+ shadowMode: z.ZodOptional<z.ZodBoolean>;
8
+ shadowDecision: z.ZodOptional<z.ZodEnum<["allow", "deny", "pending_approval"]>>;
9
+ reason: z.ZodOptional<z.ZodString>;
10
+ matchedRule: z.ZodOptional<z.ZodObject<{
11
+ id: z.ZodString;
12
+ name: z.ZodString;
13
+ priority: z.ZodOptional<z.ZodNumber>;
14
+ reason: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ id: string;
17
+ name: string;
18
+ reason?: string | undefined;
19
+ priority?: number | undefined;
20
+ }, {
21
+ id: string;
22
+ name: string;
23
+ reason?: string | undefined;
24
+ priority?: number | undefined;
25
+ }>>;
26
+ approvalId: z.ZodOptional<z.ZodString>;
27
+ rateLimitRemaining: z.ZodOptional<z.ZodNumber>;
28
+ receipt: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
29
+ }, "strip", z.ZodTypeAny, {
30
+ id: string;
31
+ decision: "allow" | "pending_approval" | "deny";
32
+ shadowMode?: boolean | undefined;
33
+ shadowDecision?: "allow" | "pending_approval" | "deny" | undefined;
34
+ reason?: string | undefined;
35
+ matchedRule?: {
36
+ id: string;
37
+ name: string;
38
+ reason?: string | undefined;
39
+ priority?: number | undefined;
40
+ } | undefined;
41
+ approvalId?: string | undefined;
42
+ rateLimitRemaining?: number | undefined;
43
+ receipt?: Record<string, unknown> | undefined;
44
+ }, {
45
+ id: string;
46
+ decision: "allow" | "pending_approval" | "deny";
47
+ shadowMode?: boolean | undefined;
48
+ shadowDecision?: "allow" | "pending_approval" | "deny" | undefined;
49
+ reason?: string | undefined;
50
+ matchedRule?: {
51
+ id: string;
52
+ name: string;
53
+ reason?: string | undefined;
54
+ priority?: number | undefined;
55
+ } | undefined;
56
+ approvalId?: string | undefined;
57
+ rateLimitRemaining?: number | undefined;
58
+ receipt?: Record<string, unknown> | undefined;
59
+ }>;
60
+ export declare const ActionStatusResponseSchema: z.ZodObject<{
61
+ id: z.ZodString;
62
+ status: z.ZodEnum<["pending", "approved", "rejected", "executed", "failed", "expired"]>;
63
+ decision: z.ZodOptional<z.ZodEnum<["allow", "deny", "pending_approval"]>>;
64
+ reason: z.ZodOptional<z.ZodString>;
65
+ tool: z.ZodOptional<z.ZodString>;
66
+ action: z.ZodOptional<z.ZodString>;
67
+ params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
68
+ approval: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
69
+ }, "strip", z.ZodTypeAny, {
70
+ id: string;
71
+ status: "pending" | "approved" | "rejected" | "expired" | "executed" | "failed";
72
+ decision?: "allow" | "pending_approval" | "deny" | undefined;
73
+ params?: Record<string, unknown> | undefined;
74
+ reason?: string | undefined;
75
+ tool?: string | undefined;
76
+ action?: string | undefined;
77
+ approval?: Record<string, unknown> | undefined;
78
+ }, {
79
+ id: string;
80
+ status: "pending" | "approved" | "rejected" | "expired" | "executed" | "failed";
81
+ decision?: "allow" | "pending_approval" | "deny" | undefined;
82
+ params?: Record<string, unknown> | undefined;
83
+ reason?: string | undefined;
84
+ tool?: string | undefined;
85
+ action?: string | undefined;
86
+ approval?: Record<string, unknown> | undefined;
87
+ }>;
88
+ export declare class SonnevGate {
89
+ private readonly apiKey;
90
+ private readonly endpoint;
91
+ private readonly timeoutMs;
92
+ private readonly pollIntervalMs;
93
+ private readonly approvalTimeoutMs;
94
+ constructor(config?: GateConfig);
95
+ /**
96
+ * Pre-execution policy check.
97
+ * Fails closed if the gateway is unreachable.
98
+ */
99
+ checkAction(params: {
100
+ tool: string;
101
+ action?: string;
102
+ params?: Record<string, unknown>;
103
+ }): Promise<CheckActionResponse>;
104
+ /**
105
+ * Queries status of an action request.
106
+ */
107
+ getActionStatus(actionId: string): Promise<ActionStatusResponse>;
108
+ /**
109
+ * Waits for a pending approval action until approved, rejected, or expired.
110
+ * Retries transient network/5xx errors while polling until approval timeout expires.
111
+ */
112
+ waitForApproval(actionId: string, options?: {
113
+ timeoutMs?: number;
114
+ pollIntervalMs?: number;
115
+ }): Promise<ActionStatusResponse>;
116
+ /**
117
+ * Reports execution outcome back to the gateway.
118
+ */
119
+ reportResult(actionId: string, result: {
120
+ status: "success" | "failure" | "failed_verification" | "recovered";
121
+ output?: unknown;
122
+ error?: string;
123
+ durationMs?: number;
124
+ }): Promise<void>;
125
+ /**
126
+ * Wraps an arbitrary tool execution function with policy check, human approval,
127
+ * verification, and outcome reporting.
128
+ *
129
+ * @param toolFn Function implementing the tool logic
130
+ * @param options Policy options including tool name, verification, and recovery
131
+ */
132
+ wrap<TParams = any, TResult = any>(toolFn: (params: TParams) => Promise<TResult> | TResult, options: WrapOptions<TParams, TResult>): (params: TParams) => Promise<TResult>;
133
+ /**
134
+ * Verifies an Ed25519 signed receipt offline without network calls.
135
+ */
136
+ verifyReceipt(receipt: SignedReceipt, expectedPublicKey?: string): ReceiptVerificationResult;
137
+ }
138
+ //# sourceMappingURL=gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,yBAAyB,EAC/B,MAAM,gBAAgB,CAAC;AAUxB,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACnB,UAAU,EACV,WAAW,EACZ,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAiBpC,CAAC;AAGH,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;EASrC,CAAC;AAEH,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;gBAE/B,MAAM,GAAE,UAAe;IAwBnC;;;OAGG;IACU,WAAW,CAAC,MAAM,EAAE;QAC/B,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC,GAAG,OAAO,CAAC,mBAAmB,CAAC;IA0DhC;;OAEG;IACU,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAoC7E;;;OAGG;IACU,eAAe,CAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,GACxD,OAAO,CAAC,oBAAoB,CAAC;IAqChC;;OAEG;IACU,YAAY,CACvB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE;QACN,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,qBAAqB,GAAG,WAAW,CAAC;QACpE,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,GACA,OAAO,CAAC,IAAI,CAAC;IAkBhB;;;;;;OAMG;IACI,IAAI,CAAC,OAAO,GAAG,GAAG,EAAE,OAAO,GAAG,GAAG,EACtC,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,EACvD,OAAO,EAAE,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,GACrC,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC;IAkIxC;;OAEG;IACI,aAAa,CAClB,OAAO,EAAE,aAAa,EACtB,iBAAiB,CAAC,EAAE,MAAM,GACzB,yBAAyB;CAG7B"}
package/dist/gate.js ADDED
@@ -0,0 +1,308 @@
1
+ import { z } from "zod";
2
+ import { verifySignedReceipt, } from "@sonnevhq/core";
3
+ import { ApprovalRejectedError, ApprovalTimeoutError, GateError, GateUnreachableError, PolicyDeniedError, VerificationFailedError, } from "./errors.js";
4
+ export const CheckActionResponseSchema = z.object({
5
+ id: z.string().min(1),
6
+ decision: z.enum(["allow", "deny", "pending_approval"]),
7
+ shadowMode: z.boolean().optional(),
8
+ shadowDecision: z.enum(["allow", "deny", "pending_approval"]).optional(),
9
+ reason: z.string().optional(),
10
+ matchedRule: z
11
+ .object({
12
+ id: z.string(),
13
+ name: z.string(),
14
+ priority: z.number().optional(),
15
+ reason: z.string().optional(),
16
+ })
17
+ .optional(),
18
+ approvalId: z.string().optional(),
19
+ rateLimitRemaining: z.number().optional(),
20
+ receipt: z.record(z.unknown()).optional(),
21
+ });
22
+ export const ActionStatusResponseSchema = z.object({
23
+ id: z.string().min(1),
24
+ status: z.enum(["pending", "approved", "rejected", "executed", "failed", "expired"]),
25
+ decision: z.enum(["allow", "deny", "pending_approval"]).optional(),
26
+ reason: z.string().optional(),
27
+ tool: z.string().optional(),
28
+ action: z.string().optional(),
29
+ params: z.record(z.unknown()).optional(),
30
+ approval: z.record(z.unknown()).optional(),
31
+ });
32
+ export class SonnevGate {
33
+ apiKey;
34
+ endpoint;
35
+ timeoutMs;
36
+ pollIntervalMs;
37
+ approvalTimeoutMs;
38
+ constructor(config = {}) {
39
+ const key = config.apiKey ||
40
+ (typeof process !== "undefined" ? process.env?.["SONNEV_GATE_API_KEY"] : undefined);
41
+ if (!key) {
42
+ throw new GateError("SONNEV Gate API key is required. Set SONNEV_GATE_API_KEY environment variable or pass apiKey in constructor.");
43
+ }
44
+ this.apiKey = key;
45
+ this.endpoint = (config.endpoint ||
46
+ (typeof process !== "undefined" ? process.env?.["SONNEV_GATE_URL"] : undefined) ||
47
+ "http://localhost:3000").replace(/\/$/, "");
48
+ this.timeoutMs = config.timeoutMs ?? 5000;
49
+ this.pollIntervalMs = config.pollIntervalMs ?? 1000;
50
+ this.approvalTimeoutMs = config.approvalTimeoutMs ?? 900_000; // 15 mins
51
+ }
52
+ /**
53
+ * Pre-execution policy check.
54
+ * Fails closed if the gateway is unreachable.
55
+ */
56
+ async checkAction(params) {
57
+ const controller = new AbortController();
58
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
59
+ try {
60
+ const headers = {
61
+ Authorization: `Bearer ${this.apiKey}`,
62
+ "Content-Type": "application/json",
63
+ "User-Agent": "SONNEV-Gate-SDK/0.1.0",
64
+ };
65
+ const response = await fetch(`${this.endpoint}/api/v1/actions/check`, {
66
+ method: "POST",
67
+ headers,
68
+ body: JSON.stringify({
69
+ tool: params.tool,
70
+ action: params.action,
71
+ params: params.params || {},
72
+ }),
73
+ signal: controller.signal,
74
+ });
75
+ if (response.status === 401 || response.status === 403) {
76
+ const errorData = await response.json().catch(() => ({}));
77
+ throw new GateError(errorData.error || `Authentication failed with status ${response.status}`);
78
+ }
79
+ if (!response.ok) {
80
+ throw new GateUnreachableError(`Gateway returned non-success HTTP status ${response.status}. Failing closed.`);
81
+ }
82
+ const json = await response.json().catch(() => ({}));
83
+ const parseResult = CheckActionResponseSchema.safeParse(json);
84
+ if (!parseResult.success) {
85
+ throw new GateUnreachableError(`Gateway returned invalid response format: ${parseResult.error.message}. Failing closed.`);
86
+ }
87
+ return parseResult.data;
88
+ }
89
+ catch (err) {
90
+ if (err instanceof GateError) {
91
+ throw err;
92
+ }
93
+ // Any network drop, DNS failure, or aborted timeout fails closed
94
+ throw new GateUnreachableError(`Failed to reach SONNEV Gate at ${this.endpoint}: ${err.message}. Failing closed.`);
95
+ }
96
+ finally {
97
+ clearTimeout(timeout);
98
+ }
99
+ }
100
+ /**
101
+ * Queries status of an action request.
102
+ */
103
+ async getActionStatus(actionId) {
104
+ const controller = new AbortController();
105
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
106
+ try {
107
+ const response = await fetch(`${this.endpoint}/api/v1/actions/${actionId}`, {
108
+ method: "GET",
109
+ headers: {
110
+ Authorization: `Bearer ${this.apiKey}`,
111
+ },
112
+ signal: controller.signal,
113
+ });
114
+ if (!response.ok) {
115
+ throw new GateUnreachableError(`Failed to get action status (${response.status})`);
116
+ }
117
+ const json = await response.json().catch(() => ({}));
118
+ const parseResult = ActionStatusResponseSchema.safeParse(json);
119
+ if (!parseResult.success) {
120
+ throw new GateUnreachableError(`Gateway returned invalid status format: ${parseResult.error.message}`);
121
+ }
122
+ return parseResult.data;
123
+ }
124
+ catch (err) {
125
+ if (err instanceof GateError)
126
+ throw err;
127
+ throw new GateUnreachableError(`Gateway unreachable: ${err.message}`);
128
+ }
129
+ finally {
130
+ clearTimeout(timeout);
131
+ }
132
+ }
133
+ /**
134
+ * Waits for a pending approval action until approved, rejected, or expired.
135
+ * Retries transient network/5xx errors while polling until approval timeout expires.
136
+ */
137
+ async waitForApproval(actionId, options) {
138
+ const maxWait = options?.timeoutMs ?? this.approvalTimeoutMs;
139
+ const interval = options?.pollIntervalMs ?? this.pollIntervalMs;
140
+ const startTime = Date.now();
141
+ while (Date.now() - startTime < maxWait) {
142
+ await sleep(interval);
143
+ let status;
144
+ try {
145
+ status = await this.getActionStatus(actionId);
146
+ }
147
+ catch (err) {
148
+ // Retry transient errors while polling until maxWait expires
149
+ if (Date.now() - startTime < maxWait) {
150
+ continue;
151
+ }
152
+ throw err;
153
+ }
154
+ if (status.status === "approved" || (status.decision === "allow" && status.status !== "pending")) {
155
+ return status;
156
+ }
157
+ if (status.status === "rejected" || status.decision === "deny") {
158
+ throw new ApprovalRejectedError(actionId, status.reason);
159
+ }
160
+ if (status.status === "expired") {
161
+ throw new ApprovalTimeoutError("Approval request expired", actionId);
162
+ }
163
+ }
164
+ throw new ApprovalTimeoutError(`Approval wait timed out after ${maxWait}ms`, actionId);
165
+ }
166
+ /**
167
+ * Reports execution outcome back to the gateway.
168
+ */
169
+ async reportResult(actionId, result) {
170
+ try {
171
+ await fetch(`${this.endpoint}/api/v1/actions/${actionId}/result`, {
172
+ method: "POST",
173
+ headers: {
174
+ Authorization: `Bearer ${this.apiKey}`,
175
+ "Content-Type": "application/json",
176
+ },
177
+ body: JSON.stringify(result),
178
+ });
179
+ }
180
+ catch (err) {
181
+ console.warn(`[SONNEV Gate] Failed to report action result for ${actionId}:`, err.message);
182
+ }
183
+ }
184
+ /**
185
+ * Wraps an arbitrary tool execution function with policy check, human approval,
186
+ * verification, and outcome reporting.
187
+ *
188
+ * @param toolFn Function implementing the tool logic
189
+ * @param options Policy options including tool name, verification, and recovery
190
+ */
191
+ wrap(toolFn, options) {
192
+ return async (params) => {
193
+ // 1. Pre-execution policy check (fails closed if unreachable)
194
+ const check = await this.checkAction({
195
+ tool: options.tool,
196
+ action: options.action,
197
+ params: params || {},
198
+ });
199
+ // 2. Immediate Deny (only in enforce mode)
200
+ if (check.decision === "deny" && !check.shadowMode) {
201
+ throw new PolicyDeniedError(check.reason, check.id, check.matchedRule);
202
+ }
203
+ // 3. Human Approval Wait (only in enforce mode)
204
+ let approvedStatus = null;
205
+ if (check.decision === "pending_approval" && !check.shadowMode) {
206
+ approvedStatus = await this.waitForApproval(check.id, {
207
+ timeoutMs: options.approvalTimeoutMs,
208
+ pollIntervalMs: options.pollIntervalMs,
209
+ });
210
+ }
211
+ // 4. Execution ONLY if strictly authorized or shadow mode
212
+ const isAllowed = check.decision === "allow" || check.shadowMode === true;
213
+ const isApproved = approvedStatus?.status === "approved" || approvedStatus?.decision === "allow";
214
+ if (!isAllowed && !isApproved) {
215
+ throw new PolicyDeniedError(check.reason || "Action was not authorized for execution", check.id, check.matchedRule);
216
+ }
217
+ // 5. Execution
218
+ const startTime = Date.now();
219
+ let result;
220
+ try {
221
+ result = await toolFn(params);
222
+ }
223
+ catch (execError) {
224
+ const durationMs = Date.now() - startTime;
225
+ await this.reportResult(check.id, {
226
+ status: "failure",
227
+ error: execError.message || String(execError),
228
+ durationMs,
229
+ });
230
+ if (options.recover) {
231
+ const recovered = await options.recover(execError, params);
232
+ if (recovered !== undefined) {
233
+ await this.reportResult(check.id, {
234
+ status: "recovered",
235
+ output: recovered,
236
+ durationMs,
237
+ });
238
+ return recovered;
239
+ }
240
+ }
241
+ throw execError;
242
+ }
243
+ const durationMs = Date.now() - startTime;
244
+ // 5. Post-Execution Verification Hook
245
+ if (options.verify) {
246
+ let isValid = true;
247
+ let failureReason = "Verification criteria not satisfied";
248
+ try {
249
+ const verifyOutput = await options.verify(result, params);
250
+ if (typeof verifyOutput === "boolean") {
251
+ isValid = verifyOutput;
252
+ }
253
+ else if (verifyOutput && typeof verifyOutput === "object") {
254
+ isValid = verifyOutput.valid;
255
+ if (verifyOutput.reason)
256
+ failureReason = verifyOutput.reason;
257
+ }
258
+ }
259
+ catch (verifyErr) {
260
+ isValid = false;
261
+ failureReason = `Verification hook threw: ${verifyErr.message}`;
262
+ }
263
+ if (!isValid) {
264
+ await this.reportResult(check.id, {
265
+ status: "failed_verification",
266
+ error: failureReason,
267
+ output: result,
268
+ durationMs,
269
+ });
270
+ if (options.recover) {
271
+ try {
272
+ const recovered = await options.recover(new Error(failureReason), params);
273
+ if (recovered !== undefined) {
274
+ await this.reportResult(check.id, {
275
+ status: "recovered",
276
+ output: recovered,
277
+ durationMs,
278
+ });
279
+ return recovered;
280
+ }
281
+ }
282
+ catch (recoverErr) {
283
+ console.warn("[SONNEV Gate] Recovery hook threw after verification failure:", recoverErr);
284
+ }
285
+ }
286
+ throw new VerificationFailedError(failureReason, check.id);
287
+ }
288
+ }
289
+ // 6. Report Success Outcome
290
+ await this.reportResult(check.id, {
291
+ status: "success",
292
+ output: result,
293
+ durationMs,
294
+ });
295
+ return result;
296
+ };
297
+ }
298
+ /**
299
+ * Verifies an Ed25519 signed receipt offline without network calls.
300
+ */
301
+ verifyReceipt(receipt, expectedPublicKey) {
302
+ return verifySignedReceipt(receipt, expectedPublicKey);
303
+ }
304
+ }
305
+ function sleep(ms) {
306
+ return new Promise((resolve) => setTimeout(resolve, ms));
307
+ }
308
+ //# sourceMappingURL=gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gate.js","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,mBAAmB,GAGpB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,qBAAqB,EAC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W;oCACnB,MAAM,EAAE,SAAS;oCACjB,UAAU;iCACX,CAAC,CAAC;gCACH,OAAO,SAAS,CAAC;4BACnB,CAAC;wBACH,CAAC;wBAAC,OAAO,UAAU,EAAE,CAAC;4BACpB,OAAO,CAAC,IAAI,CACV,+DAA+D,EAC/D,UAAU,CACX,CAAC;wBACJ,CAAC;oBACH,CAAC;oBAED,MAAM,IAAI,uBAAuB,CAAC,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC;gBAC7D,CAAC;YACH,CAAC;YAED,4BAA4B;YAC5B,MAAM,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,EAAE;gBAChC,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,MAAM;gBACd,UAAU;aACX,CAAC,CAAC;YAEH,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,aAAa,CAClB,OAAsB,EACtB,iBAA0B;QAE1B,OAAO,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IACzD,CAAC;CACF;AAGD,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { SonnevGate } from "./gate.js";
2
+ export * from "./errors.js";
3
+ export * from "./types.js";
4
+ export * from "./adapters/index.js";
5
+ export { verifySignedReceipt, exportReceiptToJson, parseReceiptFromJson, generateReceiptKeyPair, createSignedReceipt, type SignedReceipt, type SignedReceiptPayload, type ReceiptVerificationResult, } from "@sonnevhq/core";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,sBAAsB,EACtB,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,GAC/B,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { SonnevGate } from "./gate.js";
2
+ export * from "./errors.js";
3
+ export * from "./types.js";
4
+ export * from "./adapters/index.js";
5
+ export { verifySignedReceipt, exportReceiptToJson, parseReceiptFromJson, generateReceiptKeyPair, createSignedReceipt, } from "@sonnevhq/core";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,sBAAsB,EACtB,mBAAmB,GAIpB,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,96 @@
1
+ export interface GateConfig {
2
+ /**
3
+ * Agent API key. Defaults to process.env.SONNEV_GATE_API_KEY
4
+ */
5
+ apiKey?: string;
6
+ /**
7
+ * Gateway Base URL. Defaults to process.env.SONNEV_GATE_URL || "http://localhost:3000"
8
+ */
9
+ endpoint?: string;
10
+ /**
11
+ * Request timeout for gateway communication in ms (default: 5000ms).
12
+ */
13
+ timeoutMs?: number;
14
+ /**
15
+ * Polling frequency in ms when waiting on human approvals (default: 1000ms).
16
+ */
17
+ pollIntervalMs?: number;
18
+ /**
19
+ * Maximum duration in ms to wait for an approval before timing out (default: 900,000ms = 15m).
20
+ */
21
+ approvalTimeoutMs?: number;
22
+ }
23
+ export interface WrapOptions<TParams, TResult> {
24
+ /**
25
+ * Tool identifier as registered in SONNEV Gate policies (e.g. "send_payment").
26
+ */
27
+ tool: string;
28
+ /**
29
+ * Optional specific action name within the tool (e.g. "create", "delete").
30
+ */
31
+ action?: string;
32
+ /**
33
+ * Override polling interval for this tool in ms.
34
+ */
35
+ pollIntervalMs?: number;
36
+ /**
37
+ * Override approval wait timeout for this tool in ms.
38
+ */
39
+ approvalTimeoutMs?: number;
40
+ /**
41
+ * Post-execution verification hook.
42
+ * Runs immediately after the tool function returns.
43
+ * Return false or { valid: false, reason: "..." } to mark action failed_verification,
44
+ * alert the incident room, and trigger an automated agent pause.
45
+ */
46
+ verify?: (result: TResult, params: TParams) => Promise<boolean | {
47
+ valid: boolean;
48
+ reason?: string;
49
+ }> | boolean | {
50
+ valid: boolean;
51
+ reason?: string;
52
+ };
53
+ /**
54
+ * Optional recovery hook called when execution or verification fails.
55
+ */
56
+ recover?: (error: unknown, params: TParams) => Promise<TResult | void> | TResult | void;
57
+ }
58
+ export interface CheckActionParams {
59
+ tool: string;
60
+ action?: string;
61
+ params?: Record<string, unknown>;
62
+ shadowMode?: boolean;
63
+ }
64
+ import type { SignedReceipt } from "@sonnevhq/core";
65
+ export interface CheckActionResponse {
66
+ id: string;
67
+ decision: "allow" | "pending_approval" | "deny";
68
+ shadowMode?: boolean;
69
+ shadowDecision?: "allow" | "pending_approval" | "deny";
70
+ reason: string;
71
+ matchedRule?: {
72
+ id: string;
73
+ name: string;
74
+ priority: number;
75
+ reason: string;
76
+ };
77
+ approvalId?: string;
78
+ expiresAt?: string;
79
+ receipt?: SignedReceipt;
80
+ }
81
+ export interface ActionStatusResponse {
82
+ id: string;
83
+ tool: string;
84
+ action?: string;
85
+ decision: "allow" | "pending_approval" | "deny";
86
+ status: "allowed" | "pending" | "approved" | "rejected" | "expired" | "executed" | "failed_verification";
87
+ reason?: string;
88
+ approval?: {
89
+ id: string;
90
+ status: string;
91
+ approverEmail?: string;
92
+ note?: string;
93
+ expiresAt: string;
94
+ };
95
+ }
96
+ //# sourceMappingURL=types.d.ts.map