@forg3t/sdk 0.1.3 → 0.1.5

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/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,17 +17,42 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var index_exports = {};
22
32
  __export(index_exports, {
33
+ AccessLevel: () => AccessLevel,
34
+ DEFAULT_WHITEBOX_QUALITY_GATE_POLICY: () => DEFAULT_WHITEBOX_QUALITY_GATE_POLICY,
35
+ ENTERPRISE_STRICT_QUALITY_GATE_POLICY: () => ENTERPRISE_STRICT_QUALITY_GATE_POLICY,
23
36
  Forg3tApiConnectionError: () => Forg3tApiConnectionError,
24
37
  Forg3tAuthenticationError: () => Forg3tAuthenticationError,
25
38
  Forg3tClient: () => Forg3tClient,
26
39
  Forg3tError: () => Forg3tError,
27
40
  Forg3tNotFoundError: () => Forg3tNotFoundError,
28
- Forg3tRateLimitError: () => Forg3tRateLimitError
41
+ Forg3tNotImplementedError: () => Forg3tNotImplementedError,
42
+ Forg3tRateLimitError: () => Forg3tRateLimitError,
43
+ HttpTrainingAdapter: () => HttpTrainingAdapter,
44
+ IntegrationMode: () => IntegrationMode,
45
+ IntegrationType: () => IntegrationType,
46
+ LocalCommandTrainingAdapter: () => LocalCommandTrainingAdapter,
47
+ WHITEBOX_QUALITY_GATE_PRESETS: () => WHITEBOX_QUALITY_GATE_PRESETS,
48
+ WHITEBOX_QUALITY_GATE_PRESET_EXPLANATIONS: () => WHITEBOX_QUALITY_GATE_PRESET_EXPLANATIONS,
49
+ WhiteboxWorker: () => WhiteboxWorker,
50
+ assertDeploymentCompatibility: () => assertDeploymentCompatibility,
51
+ buildModelUnlearningPayload: () => buildModelUnlearningPayload,
52
+ buildWhiteboxWorkerOptions: () => buildWhiteboxWorkerOptions,
53
+ createNativeTrainingAdapter: () => createNativeTrainingAdapter,
54
+ evaluateWhiteboxQualityGate: () => evaluateWhiteboxQualityGate,
55
+ resolveWhiteboxQualityGatePolicy: () => resolveWhiteboxQualityGatePolicy
29
56
  });
30
57
  module.exports = __toCommonJS(index_exports);
31
58
 
@@ -64,18 +91,39 @@ var Forg3tApiConnectionError = class extends Forg3tError {
64
91
  this.name = "Forg3tApiConnectionError";
65
92
  }
66
93
  };
94
+ var Forg3tNotImplementedError = class extends Forg3tError {
95
+ constructor(message, endpoint, requestId) {
96
+ super(
97
+ `Endpoint not implemented: ${endpoint || "unknown"}`,
98
+ 501,
99
+ "NOT_IMPLEMENTED",
100
+ requestId,
101
+ { endpoint }
102
+ );
103
+ this.name = "Forg3tNotImplementedError";
104
+ }
105
+ };
67
106
 
68
107
  // src/transport.ts
69
108
  var Transport = class {
70
109
  constructor(config) {
71
- this.baseUrl = config.baseUrl || typeof process !== "undefined" && process.env.FORG3T_API_URL || "";
110
+ const rawBaseUrl = config.baseUrl || typeof process !== "undefined" && process.env.FORG3T_API_URL || "";
111
+ this.baseUrl = String(rawBaseUrl).trim();
72
112
  if (!this.baseUrl) {
73
113
  throw new Error("Forg3tClient: apiUrl option or FORG3T_API_URL environment variable is required");
74
114
  }
115
+ if (!/^https?:\/\//i.test(this.baseUrl)) {
116
+ throw new Error("Forg3tClient: apiUrl must be an absolute http(s) URL");
117
+ }
75
118
  if (this.baseUrl.endsWith("/")) {
76
119
  this.baseUrl = this.baseUrl.slice(0, -1);
77
120
  }
78
- this.apiKey = config.apiKey || (typeof process !== "undefined" ? process.env.FORG3T_API_KEY : void 0);
121
+ const rawApiKey = config.apiKey || (typeof process !== "undefined" ? process.env.FORG3T_API_KEY : void 0);
122
+ const apiKey = typeof rawApiKey === "string" ? rawApiKey.trim() : rawApiKey;
123
+ this.apiKey = apiKey || void 0;
124
+ const rawBearerToken = config.bearerToken || (typeof process !== "undefined" ? process.env.FORG3T_BEARER_TOKEN : void 0);
125
+ const bearerToken = typeof rawBearerToken === "string" ? rawBearerToken.trim() : rawBearerToken;
126
+ this.bearerToken = bearerToken || void 0;
79
127
  this.timeoutMs = config.timeoutMs || 3e4;
80
128
  }
81
129
  getBaseUrl() {
@@ -84,15 +132,22 @@ var Transport = class {
84
132
  async request(path, options = {}, requestId) {
85
133
  const url = `${this.baseUrl}${path.startsWith("/") ? "" : "/"}${path}`;
86
134
  const headers = {
87
- "Content-Type": "application/json",
88
135
  "Accept": "application/json",
89
136
  ...options.headers || {}
90
137
  };
138
+ const hasExplicitContentType = Object.keys(headers).some((key) => key.toLowerCase() === "content-type");
139
+ const hasBody = options.body !== void 0 && options.body !== null;
140
+ if (hasBody && !hasExplicitContentType) {
141
+ headers["Content-Type"] = "application/json";
142
+ }
91
143
  if (this.apiKey) {
92
144
  headers["x-api-key"] = this.apiKey;
93
145
  }
146
+ if (this.bearerToken && !headers.Authorization) {
147
+ headers.Authorization = `Bearer ${this.bearerToken}`;
148
+ }
94
149
  if (requestId) {
95
- headers["x-request-id"] = requestId;
150
+ headers["x-correlation-id"] = requestId;
96
151
  }
97
152
  const controller = new AbortController();
98
153
  const timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
@@ -105,7 +160,7 @@ var Transport = class {
105
160
  // Add the abort signal
106
161
  });
107
162
  clearTimeout(timeoutId);
108
- const responseRequestId = response.headers.get("x-request-id") || requestId;
163
+ const responseRequestId = response.headers.get("x-correlation-id") || response.headers.get("x-request-id") || requestId;
109
164
  if (!response.ok) {
110
165
  let errorData = {};
111
166
  try {
@@ -143,7 +198,8 @@ var Transport = class {
143
198
  if (error instanceof Error && error.name === "AbortError") {
144
199
  throw new Forg3tApiConnectionError(`Request timeout after ${this.timeoutMs}ms`);
145
200
  }
146
- throw new Forg3tApiConnectionError(error instanceof Error ? error.message : "Network error");
201
+ const detail = error instanceof Error ? error.message : "Network error";
202
+ throw new Forg3tApiConnectionError(`Request failed for ${url}: ${detail}`);
147
203
  }
148
204
  }
149
205
  async download(url, requestId) {
@@ -154,6 +210,9 @@ var Transport = class {
154
210
  if (this.apiKey) {
155
211
  headers["x-api-key"] = this.apiKey;
156
212
  }
213
+ if (this.bearerToken && !headers.Authorization) {
214
+ headers.Authorization = `Bearer ${this.bearerToken}`;
215
+ }
157
216
  if (requestId) {
158
217
  headers["x-request-id"] = requestId;
159
218
  }
@@ -202,7 +261,8 @@ var Transport = class {
202
261
  if (error instanceof Error && error.name === "AbortError") {
203
262
  throw new Forg3tApiConnectionError(`Download timeout after ${this.timeoutMs}ms`);
204
263
  }
205
- throw new Forg3tApiConnectionError(error instanceof Error ? error.message : "Network error");
264
+ const detail = error instanceof Error ? error.message : "Network error";
265
+ throw new Forg3tApiConnectionError(`Download failed for ${url}: ${detail}`);
206
266
  }
207
267
  }
208
268
  async requestRaw(path, options = {}, requestId) {
@@ -215,6 +275,9 @@ var Transport = class {
215
275
  if (this.apiKey) {
216
276
  headers["x-api-key"] = this.apiKey;
217
277
  }
278
+ if (this.bearerToken && !headers.Authorization) {
279
+ headers.Authorization = `Bearer ${this.bearerToken}`;
280
+ }
218
281
  if (requestId) {
219
282
  headers["x-request-id"] = requestId;
220
283
  }
@@ -238,7 +301,8 @@ var Transport = class {
238
301
  if (error instanceof Error && error.name === "AbortError") {
239
302
  throw new Forg3tApiConnectionError(`Request timeout after ${this.timeoutMs}ms`);
240
303
  }
241
- throw new Forg3tApiConnectionError(error instanceof Error ? error.message : "Network error");
304
+ const detail = error instanceof Error ? error.message : "Network error";
305
+ throw new Forg3tApiConnectionError(`Raw request failed for ${url}: ${detail}`);
242
306
  }
243
307
  }
244
308
  };
@@ -249,9 +313,31 @@ var Forg3tClient = class {
249
313
  this.transport = new Transport({
250
314
  baseUrl: options.apiUrl,
251
315
  apiKey: options.apiKey,
316
+ bearerToken: options.bearerToken,
252
317
  timeoutMs: options.timeoutMs
253
318
  });
254
319
  }
320
+ // --- Authentication ---
321
+ /**
322
+ * Get the current user context and their default project.
323
+ * Used by the Admin Dashboard for bootstrap and session verification.
324
+ */
325
+ async getCurrentUser(requestId) {
326
+ return this.transport.request("/v1/me", { method: "GET" }, requestId);
327
+ }
328
+ /**
329
+ * Bootstraps a tenant for an authenticated dashboard user.
330
+ * Requires a bearer token from the user's session instead of an API key.
331
+ */
332
+ async bootstrapTenant(data, requestId) {
333
+ if (!data?.tenantName || typeof data.tenantName !== "string" || !data.tenantName.trim()) {
334
+ throw new Error("bootstrapTenant requires a non-empty tenantName.");
335
+ }
336
+ return this.transport.request("/v1/bootstrap/tenant", {
337
+ method: "POST",
338
+ body: JSON.stringify(data)
339
+ }, requestId);
340
+ }
255
341
  // --- Projects ---
256
342
  async getProjectOverview(projectId, requestId) {
257
343
  return this.transport.request(`/v1/projects/${projectId}/overview`, { method: "GET" }, requestId);
@@ -267,6 +353,42 @@ var Forg3tClient = class {
267
353
  if (filters.cursor) params.append("cursor", filters.cursor);
268
354
  return this.transport.request(`/v1/projects/${projectId}/audit?${params.toString()}`, { method: "GET" }, requestId);
269
355
  }
356
+ // --- Integrations ---
357
+ async listIntegrations(projectId, requestId) {
358
+ return this.transport.request(`/v1/projects/${projectId}/integrations`, { method: "GET" }, requestId);
359
+ }
360
+ async createIntegration(projectId, data, requestId) {
361
+ return this.transport.request(`/v1/projects/${projectId}/integrations`, {
362
+ method: "POST",
363
+ body: JSON.stringify(data)
364
+ }, requestId);
365
+ }
366
+ async testIntegration(projectId, integrationId, requestId) {
367
+ void projectId;
368
+ return this.transport.request(
369
+ `/v1/integrations/${integrationId}/test`,
370
+ { method: "POST" },
371
+ requestId
372
+ );
373
+ }
374
+ // --- Unlearning Requests ---
375
+ async createUnlearningRequest(projectId, data, requestId) {
376
+ this.assertCreateUnlearningRequest(data);
377
+ return this.transport.request(`/v1/projects/${projectId}/unlearning-requests`, {
378
+ method: "POST",
379
+ body: JSON.stringify(data)
380
+ }, requestId);
381
+ }
382
+ async listUnlearningRequests(projectId, requestId) {
383
+ return this.transport.request(`/v1/projects/${projectId}/unlearning-requests`, { method: "GET" }, requestId);
384
+ }
385
+ async getUnlearningRequest(projectId, unlearningRequestId, requestId) {
386
+ return this.transport.request(
387
+ `/v1/projects/${projectId}/unlearning-requests/${unlearningRequestId}`,
388
+ { method: "GET" },
389
+ requestId
390
+ );
391
+ }
270
392
  // --- API Keys ---
271
393
  async listApiKeys(projectId, requestId) {
272
394
  return this.transport.request(`/v1/projects/${projectId}/api-keys`, { method: "GET" }, requestId);
@@ -277,23 +399,74 @@ var Forg3tClient = class {
277
399
  body: JSON.stringify(data)
278
400
  }, requestId);
279
401
  }
280
- async rotateApiKey(keyId, requestId) {
281
- return this.transport.request(`/v1/api-keys/${keyId}/rotate`, { method: "POST" }, requestId);
402
+ async rotateApiKey(projectId, keyId, requestId) {
403
+ if (!projectId || !keyId) {
404
+ throw new Error("rotateApiKey requires both projectId and keyId.");
405
+ }
406
+ const existingKeys = await this.listApiKeys(projectId, requestId);
407
+ const current = existingKeys.find((item) => item.id === keyId);
408
+ if (!current) {
409
+ throw new Forg3tNotFoundError(`API key ${keyId} was not found in project ${projectId}.`, requestId);
410
+ }
411
+ const rotated = await this.createApiKey(projectId, {
412
+ name: current.name,
413
+ ...current.expiresAt ? { expiresAt: current.expiresAt } : {}
414
+ }, requestId);
415
+ await this.revokeApiKey(projectId, keyId, requestId);
416
+ return rotated;
282
417
  }
283
- async revokeApiKey(keyId, requestId) {
284
- return this.transport.request(`/v1/api-keys/${keyId}/revoke`, { method: "POST" }, requestId);
418
+ async revokeApiKey(projectId, keyId, requestId) {
419
+ if (!projectId || !keyId) {
420
+ throw new Error("revokeApiKey requires both projectId and keyId.");
421
+ }
422
+ return this.transport.request(`/v1/projects/${projectId}/api-keys/${keyId}`, {
423
+ method: "DELETE"
424
+ }, requestId);
285
425
  }
286
426
  // --- Jobs ---
287
427
  async submitJob(projectId, data, requestId) {
428
+ const normalizedPayload = "payload" in data ? data.payload : {
429
+ claim: data.claim,
430
+ config: data.config
431
+ };
432
+ if (data.type === "MODEL_UNLEARN") {
433
+ this.assertModelUnlearnPreflight(normalizedPayload);
434
+ }
288
435
  const res = await this.transport.request(`/v1/projects/${projectId}/jobs`, {
289
436
  method: "POST",
290
- body: JSON.stringify(data)
437
+ body: JSON.stringify({
438
+ type: data.type,
439
+ payload: normalizedPayload,
440
+ idempotencyKey: data.idempotencyKey
441
+ })
291
442
  }, requestId);
292
443
  return res;
293
444
  }
445
+ /**
446
+ * Creates a new job with deduplication support via an optional idempotency key.
447
+ * This wrapper cleanly exposes the core parameters for the production job queue.
448
+ */
449
+ async createJob(projectId, type, claim, config, idempotencyKey, requestId) {
450
+ return this.submitJob(projectId, { type, claim, config, idempotencyKey }, requestId);
451
+ }
294
452
  async getJob(projectId, jobId, requestId) {
295
453
  return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}`, { method: "GET" }, requestId);
296
454
  }
455
+ /**
456
+ * Polls the job status continuously until a terminal state (succeeded, failed, canceled) is reached,
457
+ * or the specified timeout limits are exceeded.
458
+ */
459
+ async pollJobStatus(projectId, jobId, timeoutMs = 3e5, pollIntervalMs = 2e3, requestId) {
460
+ const startTime = Date.now();
461
+ while (Date.now() - startTime < timeoutMs) {
462
+ const job = await this.getJob(projectId, jobId, requestId);
463
+ if (job.status === "succeeded" || job.status === "failed" || job.status === "canceled") {
464
+ return job;
465
+ }
466
+ await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
467
+ }
468
+ throw new Error(`pollJobStatus timed out after ${timeoutMs}ms waiting for job ${jobId} to reach a terminal state.`);
469
+ }
297
470
  async listJobs(projectId, query, requestId) {
298
471
  const params = new URLSearchParams();
299
472
  if (query?.status) params.append("status", query.status);
@@ -307,20 +480,37 @@ var Forg3tClient = class {
307
480
  body: JSON.stringify(data)
308
481
  }, requestId);
309
482
  }
310
- async heartbeatJob(projectId, jobId, requestId) {
311
- return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}/heartbeat`, { method: "POST" }, requestId);
483
+ async heartbeatJob(projectId, jobId, optionsOrRequestId, requestId) {
484
+ const options = typeof optionsOrRequestId === "string" ? {} : optionsOrRequestId || {};
485
+ const resolvedRequestId = typeof optionsOrRequestId === "string" ? optionsOrRequestId : requestId;
486
+ return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}/heartbeat`, {
487
+ method: "POST",
488
+ body: JSON.stringify({
489
+ claimId: options.claimId
490
+ })
491
+ }, resolvedRequestId);
312
492
  }
313
- async completeJob(projectId, jobId, result, requestId) {
493
+ async completeJob(projectId, jobId, result, optionsOrRequestId, requestId) {
494
+ const options = typeof optionsOrRequestId === "string" ? {} : optionsOrRequestId || {};
495
+ const resolvedRequestId = typeof optionsOrRequestId === "string" ? optionsOrRequestId : requestId;
314
496
  return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}/complete`, {
315
497
  method: "POST",
316
- body: JSON.stringify(result)
317
- }, requestId);
498
+ body: JSON.stringify({
499
+ ...result,
500
+ claimId: options.claimId || result.claimId
501
+ })
502
+ }, resolvedRequestId);
318
503
  }
319
- async failJob(projectId, jobId, error, requestId) {
504
+ async failJob(projectId, jobId, error, optionsOrRequestId, requestId) {
505
+ const options = typeof optionsOrRequestId === "string" ? {} : optionsOrRequestId || {};
506
+ const resolvedRequestId = typeof optionsOrRequestId === "string" ? optionsOrRequestId : requestId;
320
507
  return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}/fail`, {
321
508
  method: "POST",
322
- body: JSON.stringify(error)
323
- }, requestId);
509
+ body: JSON.stringify({
510
+ ...error,
511
+ claimId: options.claimId || error.claimId
512
+ })
513
+ }, resolvedRequestId);
324
514
  }
325
515
  async listDeadJobs(projectId, requestId) {
326
516
  return this.transport.request(`/v1/projects/${projectId}/jobs/dead`, { method: "GET" }, requestId);
@@ -330,7 +520,20 @@ var Forg3tClient = class {
330
520
  }
331
521
  // --- Evidence ---
332
522
  async getEvidence(jobId, requestId) {
333
- return this.transport.request(`/v1/jobs/evidence/${jobId}`, { method: "GET" }, requestId);
523
+ try {
524
+ return await this.transport.request(`/v1/jobs/${jobId}/evidence`, { method: "GET" }, requestId);
525
+ } catch (error) {
526
+ if (error instanceof Forg3tNotFoundError) {
527
+ return this.transport.request(`/v1/jobs/evidence/${jobId}`, { method: "GET" }, requestId);
528
+ }
529
+ throw error;
530
+ }
531
+ }
532
+ async getEvidenceJson(jobId, requestId) {
533
+ return this.transport.request(`/v1/jobs/${jobId}/evidence.json`, { method: "GET" }, requestId);
534
+ }
535
+ async getArtifactDownloadUrl(projectId, jobId, filename, requestId) {
536
+ return this.transport.request(`/v1/projects/${projectId}/jobs/${jobId}/artifacts/${filename}/download`, { method: "GET" }, requestId);
334
537
  }
335
538
  async getEvidencePdf(jobId, options) {
336
539
  const response = await this.transport.requestRaw(`/v1/jobs/${jobId}/evidence.pdf`, { method: "GET" });
@@ -354,26 +557,24 @@ var Forg3tClient = class {
354
557
  return result;
355
558
  }
356
559
  async savePdfToFile(data, filePath) {
357
- if (typeof global !== "undefined" && typeof require !== "undefined") {
358
- try {
359
- const fs = require("fs");
360
- const path = require("path");
361
- const dir = path.dirname(filePath);
362
- if (!fs.existsSync(dir)) {
363
- fs.mkdirSync(dir, { recursive: true });
364
- }
365
- fs.writeFileSync(filePath, Buffer.from(data));
366
- } catch (error) {
367
- console.error("Failed to save PDF to file:", error);
368
- throw error;
369
- }
370
- } else {
560
+ const isNode = typeof process !== "undefined" && typeof process.versions?.node === "string";
561
+ if (!isNode) {
371
562
  throw new Error("File saving is only supported in Node.js environment");
372
563
  }
564
+ try {
565
+ const fs = await import("fs/promises");
566
+ const path = await import("path");
567
+ const dir = path.dirname(filePath);
568
+ await fs.mkdir(dir, { recursive: true });
569
+ await fs.writeFile(filePath, data);
570
+ } catch (error) {
571
+ console.error("Failed to save PDF to file:", error);
572
+ throw error;
573
+ }
373
574
  }
374
575
  /**
375
576
  * @deprecated The current Control Plane version does not support direct artifact downloads.
376
- * Please use getEvidence() to retrieve the artifact metadata and content.
577
+ * Please use getArtifactDownloadUrl(), getEvidencePdf(), or getEvidenceJson().
377
578
  */
378
579
  async downloadEvidenceArtifact(_jobId, _requestId) {
379
580
  throw new Error("Artifact download not supported by current API version. Use getEvidence() to retrieve content.");
@@ -444,13 +645,905 @@ var Forg3tClient = class {
444
645
  body: JSON.stringify(result)
445
646
  }, requestId);
446
647
  }
648
+ assertCreateUnlearningRequest(data) {
649
+ if (!data.target?.value?.trim()) {
650
+ throw new Error("createUnlearningRequest: target.value is required.");
651
+ }
652
+ if (typeof data.target.aliases !== "undefined") {
653
+ if (!Array.isArray(data.target.aliases) || data.target.aliases.some((alias) => typeof alias !== "string" || !alias.trim())) {
654
+ throw new Error("createUnlearningRequest: target.aliases must be an array of non-empty strings when provided.");
655
+ }
656
+ }
657
+ if (data.accessLevel === "layer_a_only") {
658
+ const target = data.execution?.target;
659
+ if (!target) {
660
+ throw new Error("createUnlearningRequest: execution.target is required for layer_a_only.");
661
+ }
662
+ if ((target.provider === "openai" || target.provider === "groq") && !target.model?.trim()) {
663
+ throw new Error(`createUnlearningRequest: execution.target.model is required when provider=${target.provider}.`);
664
+ }
665
+ if ((target.provider === "http_generic" || target.provider === "custom") && !target.endpoint?.trim()) {
666
+ throw new Error(`createUnlearningRequest: execution.target.endpoint is required when provider=${target.provider}.`);
667
+ }
668
+ }
669
+ if (data.accessLevel === "layer_a_and_b") {
670
+ if (!data.execution?.model?.uri?.trim()) {
671
+ throw new Error("createUnlearningRequest: execution.model.uri is required for layer_a_and_b.");
672
+ }
673
+ const targetTokenIds = data.execution?.plan?.hyperparameters?.target_token_ids;
674
+ const tokenizerUri = data.execution?.model?.metadata?.tokenizer_uri;
675
+ const localizationMode = String(
676
+ data.execution?.plan?.hyperparameters?.localization_mode || data.execution?.plan?.hyperparameters?.localizationMode || ""
677
+ ).trim().toLowerCase();
678
+ const allowMissingLocalization = String(process.env.FORG3T_ALLOW_MODEL_UNLEARN_WITHOUT_LOCALIZATION_INPUT || "").toLowerCase() === "true";
679
+ const hasTokenIds = Array.isArray(targetTokenIds) && targetTokenIds.some((value) => Number.isInteger(value) && Number(value) >= 0);
680
+ const hasTokenizer = typeof tokenizerUri === "string" && tokenizerUri.trim().length > 0;
681
+ if (localizationMode === "explicit_token_ids" && !hasTokenIds) {
682
+ throw new Error("createUnlearningRequest: localization_mode=explicit_token_ids requires target token IDs.");
683
+ }
684
+ if (localizationMode === "tokenizer_auto" && !hasTokenizer) {
685
+ throw new Error("createUnlearningRequest: localization_mode=tokenizer_auto requires execution.model.metadata.tokenizer_uri.");
686
+ }
687
+ if (localizationMode === "hybrid") {
688
+ if (!hasTokenIds) {
689
+ throw new Error("createUnlearningRequest: localization_mode=hybrid requires target token IDs.");
690
+ }
691
+ if (!hasTokenizer) {
692
+ throw new Error("createUnlearningRequest: localization_mode=hybrid requires execution.model.metadata.tokenizer_uri.");
693
+ }
694
+ }
695
+ if (!allowMissingLocalization && !hasTokenIds && !hasTokenizer) {
696
+ throw new Error(
697
+ "createUnlearningRequest: layer_a_and_b requires localization input. Provide target token IDs (execution.plan.hyperparameters.target_token_ids) or tokenizer URI (execution.model.metadata.tokenizer_uri)."
698
+ );
699
+ }
700
+ }
701
+ }
702
+ assertModelUnlearnPreflight(payload) {
703
+ if (!payload || typeof payload !== "object") {
704
+ throw new Error("MODEL_UNLEARN preflight failed: payload must be an object.");
705
+ }
706
+ const asRecord = payload;
707
+ const config = asRecord.config && typeof asRecord.config === "object" && !Array.isArray(asRecord.config) ? asRecord.config : null;
708
+ if (!config) {
709
+ throw new Error("MODEL_UNLEARN preflight failed: payload.config is required.");
710
+ }
711
+ const targetConfig = config.target_config && typeof config.target_config === "object" && !Array.isArray(config.target_config) ? config.target_config : null;
712
+ const parameters = config.parameters && typeof config.parameters === "object" && !Array.isArray(config.parameters) ? config.parameters : null;
713
+ const modelFromTargetConfig = targetConfig?.model && typeof targetConfig.model === "object" && !Array.isArray(targetConfig.model) ? targetConfig.model : null;
714
+ const modelFromParameters = parameters?.model && typeof parameters.model === "object" && !Array.isArray(parameters.model) ? parameters.model : null;
715
+ const model = modelFromParameters || modelFromTargetConfig;
716
+ const modelUri = typeof model?.uri === "string" ? model.uri.trim() : "";
717
+ if (!modelUri) {
718
+ throw new Error("MODEL_UNLEARN preflight failed: config.target_config.model.uri (or config.parameters.model.uri) is required.");
719
+ }
720
+ const target = parameters?.target && typeof parameters.target === "object" && !Array.isArray(parameters.target) ? parameters.target : null;
721
+ const tokenIds = target?.tokenIds;
722
+ const hasTokenIds = Array.isArray(tokenIds) && tokenIds.some((value) => Number.isInteger(value) && Number(value) >= 0);
723
+ const tokenizerFromTargetConfig = targetConfig?.tokenizer && typeof targetConfig.tokenizer === "object" && !Array.isArray(targetConfig.tokenizer) ? targetConfig.tokenizer : null;
724
+ const tokenizerFromParameters = parameters?.tokenizer && typeof parameters.tokenizer === "object" && !Array.isArray(parameters.tokenizer) ? parameters.tokenizer : null;
725
+ const tokenizerFromModel = model?.tokenizer && typeof model.tokenizer === "object" && !Array.isArray(model.tokenizer) ? model.tokenizer : null;
726
+ const tokenizerCandidates = [
727
+ tokenizerFromTargetConfig?.uri,
728
+ tokenizerFromParameters?.uri,
729
+ parameters?.tokenizer_uri,
730
+ model?.tokenizer_uri,
731
+ tokenizerFromModel?.uri
732
+ ];
733
+ const hasTokenizerUri = tokenizerCandidates.some((candidate) => typeof candidate === "string" && candidate.trim().length > 0);
734
+ const allowMissingLocalization = String(process.env.FORG3T_ALLOW_MODEL_UNLEARN_WITHOUT_LOCALIZATION_INPUT || "").toLowerCase() === "true";
735
+ if (!allowMissingLocalization && !hasTokenIds && !hasTokenizerUri) {
736
+ throw new Error(
737
+ "MODEL_UNLEARN preflight failed: missing localization input. Provide config.parameters.target.tokenIds or a tokenizer URI in config.target_config.tokenizer.uri / config.parameters.tokenizer.uri / config.parameters.tokenizer_uri / model.tokenizer_uri."
738
+ );
739
+ }
740
+ }
741
+ };
742
+
743
+ // src/types.ts
744
+ var IntegrationType = /* @__PURE__ */ ((IntegrationType2) => {
745
+ IntegrationType2["SLACK"] = "slack";
746
+ IntegrationType2["CONFLUENCE"] = "confluence";
747
+ IntegrationType2["GOOGLE_DRIVE"] = "google_drive";
748
+ IntegrationType2["ZENDESK"] = "zendesk";
749
+ IntegrationType2["NOTION"] = "notion";
750
+ IntegrationType2["LANGCHAIN"] = "langchain";
751
+ IntegrationType2["OPENROUTER"] = "openrouter";
752
+ IntegrationType2["OPENAI"] = "openai";
753
+ IntegrationType2["CUSTOM"] = "custom";
754
+ return IntegrationType2;
755
+ })(IntegrationType || {});
756
+ var IntegrationMode = /* @__PURE__ */ ((IntegrationMode2) => {
757
+ IntegrationMode2["MODE_A_BLACKBOX"] = "mode_a_blackbox";
758
+ IntegrationMode2["MODE_B_WHITEBOX"] = "mode_b_whitebox";
759
+ return IntegrationMode2;
760
+ })(IntegrationMode || {});
761
+ var AccessLevel = /* @__PURE__ */ ((AccessLevel2) => {
762
+ AccessLevel2["LAYER_A_ONLY"] = "layer_a_only";
763
+ AccessLevel2["LAYER_A_AND_B"] = "layer_a_and_b";
764
+ return AccessLevel2;
765
+ })(AccessLevel || {});
766
+
767
+ // src/whitebox.ts
768
+ var crypto = __toESM(require("crypto"));
769
+
770
+ // src/quality-gate.ts
771
+ var WHITEBOX_QUALITY_GATE_PRESETS = {
772
+ balanced: {
773
+ requireEvaluation: false,
774
+ maxLeakScore: 0.05,
775
+ allowedVerdicts: ["PASS", "REVIEW"],
776
+ minChangedTensorCount: 1,
777
+ minChangedParamCount: 1,
778
+ requireModelHashTransition: true,
779
+ requireLocalization: false,
780
+ minLocalizationTokenCoverage: 0.5,
781
+ minLocalizedParamCount: 1,
782
+ minLocalizationConfidence: 0.6
783
+ },
784
+ strict: {
785
+ requireEvaluation: true,
786
+ maxLeakScore: 0.03,
787
+ allowedVerdicts: ["PASS"],
788
+ minChangedTensorCount: 1,
789
+ minChangedParamCount: 1,
790
+ requireModelHashTransition: true,
791
+ requireLocalization: true,
792
+ minLocalizationTokenCoverage: 0.5,
793
+ minLocalizedParamCount: 1,
794
+ minLocalizationConfidence: 0.6
795
+ }
796
+ };
797
+ var WHITEBOX_QUALITY_GATE_PRESET_EXPLANATIONS = {
798
+ balanced: "Balanced gate for faster rollout. Allows PASS/REVIEW and can run without evaluator output.",
799
+ strict: "Strict gate for production. Requires evaluator output, localization evidence, and PASS verdict only."
800
+ };
801
+ var DEFAULT_WHITEBOX_QUALITY_GATE_POLICY = {
802
+ requireEvaluation: false,
803
+ maxLeakScore: 0.05,
804
+ allowedVerdicts: ["PASS", "REVIEW"],
805
+ minChangedTensorCount: 1,
806
+ minChangedParamCount: 1,
807
+ requireModelHashTransition: true,
808
+ requireLocalization: false,
809
+ minLocalizationTokenCoverage: 0.5,
810
+ minLocalizedParamCount: 1,
811
+ minLocalizationConfidence: 0.6
812
+ };
813
+ var resolveWhiteboxQualityGatePolicy = (preset = "balanced", overrides) => {
814
+ return {
815
+ ...WHITEBOX_QUALITY_GATE_PRESETS[preset],
816
+ ...overrides || {}
817
+ };
818
+ };
819
+ var evaluateWhiteboxQualityGate = (unlearning, evaluation, policyOverrides) => {
820
+ const policy = {
821
+ ...resolveWhiteboxQualityGatePolicy("balanced"),
822
+ ...policyOverrides || {}
823
+ };
824
+ const reasons = [];
825
+ const changedTensors = Number(unlearning.changedTensorCount || 0);
826
+ const changedParams = Number(unlearning.changedParamCount || 0);
827
+ const minChangedTensors = Number(policy.minChangedTensorCount || 0);
828
+ const minChangedParams = Number(policy.minChangedParamCount || 0);
829
+ if (changedTensors < minChangedTensors) {
830
+ reasons.push(`Changed tensor count ${changedTensors} is below minimum ${minChangedTensors}.`);
831
+ }
832
+ if (minChangedParams > 0 && changedParams < minChangedParams) {
833
+ reasons.push(`Changed parameter count ${changedParams} is below minimum ${minChangedParams}.`);
834
+ }
835
+ if (policy.requireModelHashTransition) {
836
+ const before = (unlearning.modelBeforeSha256 || "").trim();
837
+ const after = (unlearning.modelAfterSha256 || "").trim();
838
+ if (!before || !after) {
839
+ reasons.push("Model hash transition is required but before/after hash is missing.");
840
+ } else if (before === after) {
841
+ reasons.push("Model hash did not change after unlearning.");
842
+ }
843
+ }
844
+ if (policy.requireEvaluation && !evaluation) {
845
+ reasons.push("Evaluation output is required but missing.");
846
+ }
847
+ if (policy.requireLocalization) {
848
+ const localization = unlearning.localization;
849
+ if (!localization) {
850
+ reasons.push("Localization output is required but missing.");
851
+ } else {
852
+ const tokenCoverage = Number(localization.tokenCoverage);
853
+ if (!Number.isFinite(tokenCoverage) || tokenCoverage < 0 || tokenCoverage > 1) {
854
+ reasons.push("Localization tokenCoverage must be a finite number in [0, 1].");
855
+ } else if (tokenCoverage < Number(policy.minLocalizationTokenCoverage)) {
856
+ reasons.push(
857
+ `Localization tokenCoverage ${tokenCoverage} is below minimum ${policy.minLocalizationTokenCoverage}.`
858
+ );
859
+ }
860
+ const localizedParamCount = Number(localization.localizedParamCount || 0);
861
+ if (localizedParamCount < Number(policy.minLocalizedParamCount)) {
862
+ reasons.push(
863
+ `Localization localizedParamCount ${localizedParamCount} is below minimum ${policy.minLocalizedParamCount}.`
864
+ );
865
+ }
866
+ const confidence = Number(localization.confidence);
867
+ if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) {
868
+ reasons.push("Localization confidence must be a finite number in [0, 1].");
869
+ } else if (confidence < Number(policy.minLocalizationConfidence)) {
870
+ reasons.push(
871
+ `Localization confidence ${confidence} is below minimum ${policy.minLocalizationConfidence}.`
872
+ );
873
+ }
874
+ }
875
+ }
876
+ if (evaluation) {
877
+ const maxLeak = Number(policy.maxLeakScore);
878
+ if (!Number.isNaN(maxLeak) && evaluation.leakScore > maxLeak) {
879
+ reasons.push(`Leak score ${evaluation.leakScore} is above max ${maxLeak}.`);
880
+ }
881
+ const allowedVerdicts = policy.allowedVerdicts || [];
882
+ if (allowedVerdicts.length > 0 && !allowedVerdicts.includes(evaluation.finalVerdict)) {
883
+ reasons.push(`Final verdict ${evaluation.finalVerdict} is not allowed by policy.`);
884
+ }
885
+ }
886
+ return {
887
+ pass: reasons.length === 0,
888
+ reasons,
889
+ policy
890
+ };
891
+ };
892
+
893
+ // src/whitebox.ts
894
+ var buildModelUnlearningPayload = (input) => {
895
+ return {
896
+ claim: input.claim,
897
+ config: {
898
+ target_adapter: input.targetAdapter || "CUSTOM_WHITEBOX_BACKEND",
899
+ target_config: {
900
+ model: input.model
901
+ },
902
+ sensitivity: input.sensitivity || "high",
903
+ parameters: {
904
+ target: input.target,
905
+ plan: input.plan,
906
+ ...input.extraParameters
907
+ }
908
+ }
909
+ };
910
+ };
911
+ var defaultLogger = {
912
+ info(message, meta) {
913
+ console.log(`[whitebox-worker] ${message}`, meta || {});
914
+ },
915
+ warn(message, meta) {
916
+ console.warn(`[whitebox-worker] ${message}`, meta || {});
917
+ },
918
+ error(message, meta) {
919
+ console.error(`[whitebox-worker] ${message}`, meta || {});
920
+ }
921
+ };
922
+ var safeErrorMessage = (error) => {
923
+ if (error instanceof Error) {
924
+ return error.message.slice(0, 500);
925
+ }
926
+ return String(error).slice(0, 500);
447
927
  };
928
+ var toJobError = (error) => {
929
+ const message = safeErrorMessage(error);
930
+ const isValidationFailure = message.startsWith("VALIDATION_ERROR:") || message.startsWith("QUALITY_GATE_FAILED:") || message.startsWith("ADAPTER_OUTPUT_INVALID:");
931
+ return {
932
+ error: {
933
+ code: isValidationFailure ? "VALIDATION_ERROR" : "WORKER_ERROR",
934
+ message
935
+ }
936
+ };
937
+ };
938
+ var toClaim = (job) => {
939
+ const claim = job.payload?.claim;
940
+ if (!claim) {
941
+ throw new Error("VALIDATION_ERROR: job payload.claim is required.");
942
+ }
943
+ if (typeof claim.claim_payload !== "string" || !claim.claim_payload.trim()) {
944
+ throw new Error("VALIDATION_ERROR: job payload.claim.claim_payload must be a non-empty string.");
945
+ }
946
+ return {
947
+ claim_type: claim.claim_type || "CONCEPT",
948
+ claim_payload: claim.claim_payload,
949
+ scope: claim.scope || "global",
950
+ assertion: claim.assertion || "must_not_be_recalled"
951
+ };
952
+ };
953
+ var toConfig = (job) => {
954
+ const config = job.payload?.config;
955
+ if (!config) {
956
+ throw new Error("VALIDATION_ERROR: job payload.config is required.");
957
+ }
958
+ return {
959
+ target_adapter: config.target_adapter || "CUSTOM_WHITEBOX_BACKEND",
960
+ target_config: config.target_config || {},
961
+ sensitivity: config.sensitivity || "high",
962
+ parameters: config.parameters || {}
963
+ };
964
+ };
965
+ var resolveModel = (config) => {
966
+ const modelFromTargetConfig = config.target_config?.model;
967
+ const modelFromParameters = config.parameters?.model;
968
+ const model = modelFromParameters || modelFromTargetConfig;
969
+ if (!model || !model.uri) {
970
+ throw new Error("MODEL_UNLEARN job missing model URI in config.target_config.model or config.parameters.model");
971
+ }
972
+ return model;
973
+ };
974
+ var resolveTarget = (claim, config) => {
975
+ const targetFromParameters = config.parameters?.target;
976
+ if (targetFromParameters) {
977
+ return targetFromParameters;
978
+ }
979
+ return {
980
+ text: claim.claim_payload,
981
+ selector: {
982
+ claim_type: claim.claim_type,
983
+ scope: claim.scope
984
+ }
985
+ };
986
+ };
987
+ var resolvePlan = (config) => {
988
+ const planFromParameters = config.parameters?.plan;
989
+ if (planFromParameters?.method) {
990
+ return planFromParameters;
991
+ }
992
+ const fallbackMethod = typeof config.parameters?.method === "string" ? config.parameters.method : "gradient_surgery";
993
+ const fallbackHyperparams = config.parameters?.hyperparameters || {};
994
+ return {
995
+ method: fallbackMethod,
996
+ hyperparameters: fallbackHyperparams
997
+ };
998
+ };
999
+ var isNonArrayRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
1000
+ var resolveQualityGatePresetCandidate = (value) => {
1001
+ if (typeof value !== "string") {
1002
+ return void 0;
1003
+ }
1004
+ const normalized = value.trim().toLowerCase();
1005
+ if (normalized === "strict" || normalized === "balanced") {
1006
+ return normalized;
1007
+ }
1008
+ return void 0;
1009
+ };
1010
+ var resolvePerJobQualityGatePolicy = (workerPolicy, config, plan) => {
1011
+ const configParams = isNonArrayRecord(config.parameters) ? config.parameters : {};
1012
+ const planHyperparameters = isNonArrayRecord(plan.hyperparameters) ? plan.hyperparameters : {};
1013
+ const preset = resolveQualityGatePresetCandidate(
1014
+ planHyperparameters.quality_gate_preset || planHyperparameters.qualityGatePreset || configParams.quality_gate_preset || configParams.qualityGatePreset
1015
+ );
1016
+ const overrideFromPlan = isNonArrayRecord(planHyperparameters.quality_gate_policy) ? planHyperparameters.quality_gate_policy : isNonArrayRecord(planHyperparameters.qualityGatePolicy) ? planHyperparameters.qualityGatePolicy : {};
1017
+ const overrideFromConfig = isNonArrayRecord(configParams.quality_gate_policy) ? configParams.quality_gate_policy : isNonArrayRecord(configParams.qualityGatePolicy) ? configParams.qualityGatePolicy : {};
1018
+ const hasOverrides = Object.keys(overrideFromPlan).length > 0 || Object.keys(overrideFromConfig).length > 0;
1019
+ if (!preset && !hasOverrides) {
1020
+ return {
1021
+ policy: workerPolicy,
1022
+ preset: null
1023
+ };
1024
+ }
1025
+ const presetPolicy = preset ? resolveWhiteboxQualityGatePolicy(preset) : workerPolicy;
1026
+ return {
1027
+ policy: {
1028
+ ...presetPolicy,
1029
+ ...overrideFromConfig,
1030
+ ...overrideFromPlan
1031
+ },
1032
+ preset: preset || null
1033
+ };
1034
+ };
1035
+ var assertUnlearningOutput = (value) => {
1036
+ const updatedModel = value?.updatedModel;
1037
+ if (!updatedModel || typeof updatedModel.uri !== "string" || !updatedModel.uri.trim()) {
1038
+ throw new Error("ADAPTER_OUTPUT_INVALID: updatedModel.uri must be a non-empty string.");
1039
+ }
1040
+ if (!Number.isFinite(value.changedTensorCount) || value.changedTensorCount < 0) {
1041
+ throw new Error("ADAPTER_OUTPUT_INVALID: changedTensorCount must be a finite number >= 0.");
1042
+ }
1043
+ if (typeof value.changedParamCount !== "undefined" && (!Number.isFinite(value.changedParamCount) || value.changedParamCount < 0)) {
1044
+ throw new Error("ADAPTER_OUTPUT_INVALID: changedParamCount must be a finite number >= 0 when provided.");
1045
+ }
1046
+ if (typeof value.metrics !== "undefined" && !isNonArrayRecord(value.metrics)) {
1047
+ throw new Error("ADAPTER_OUTPUT_INVALID: metrics must be an object when provided.");
1048
+ }
1049
+ if (typeof value.artifacts !== "undefined") {
1050
+ if (!Array.isArray(value.artifacts)) {
1051
+ throw new Error("ADAPTER_OUTPUT_INVALID: artifacts must be an array when provided.");
1052
+ }
1053
+ for (const artifact of value.artifacts) {
1054
+ if (!artifact || typeof artifact.name !== "string" || typeof artifact.uri !== "string") {
1055
+ throw new Error("ADAPTER_OUTPUT_INVALID: each artifact requires string name and uri.");
1056
+ }
1057
+ }
1058
+ }
1059
+ };
1060
+ var assertEvaluationOutput = (value) => {
1061
+ if (!value || !["PASS", "FAIL", "REVIEW"].includes(value.finalVerdict)) {
1062
+ throw new Error("ADAPTER_OUTPUT_INVALID: evaluation.finalVerdict must be PASS | FAIL | REVIEW.");
1063
+ }
1064
+ if (!Number.isFinite(value.leakScore) || value.leakScore < 0) {
1065
+ throw new Error("ADAPTER_OUTPUT_INVALID: evaluation.leakScore must be a finite number >= 0.");
1066
+ }
1067
+ if (typeof value.details !== "undefined" && !isNonArrayRecord(value.details)) {
1068
+ throw new Error("ADAPTER_OUTPUT_INVALID: evaluation.details must be an object when provided.");
1069
+ }
1070
+ };
1071
+ var sha256Hex = (value) => {
1072
+ return crypto.createHash("sha256").update(value, "utf8").digest("hex");
1073
+ };
1074
+ var buildResultClaim = (claim, mode) => {
1075
+ if (mode === "raw") {
1076
+ return claim;
1077
+ }
1078
+ const base = {
1079
+ claim_type: claim.claim_type,
1080
+ scope: claim.scope,
1081
+ assertion: claim.assertion
1082
+ };
1083
+ if (mode === "omit") {
1084
+ return base;
1085
+ }
1086
+ const payload = typeof claim.claim_payload === "string" ? claim.claim_payload : "";
1087
+ return {
1088
+ ...base,
1089
+ payload_hash: sha256Hex(payload),
1090
+ claim_payload_sha256: sha256Hex(payload),
1091
+ claim_payload_length: payload.length
1092
+ };
1093
+ };
1094
+ var WhiteboxWorker = class {
1095
+ constructor(client, adapter, options) {
1096
+ this.client = client;
1097
+ this.adapter = adapter;
1098
+ this.options = options;
1099
+ this.running = false;
1100
+ this.loopPromise = null;
1101
+ this.pollIntervalMs = options.pollIntervalMs ?? 2e3;
1102
+ this.claimBatchSize = options.claimBatchSize ?? 1;
1103
+ this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? 2e4;
1104
+ this.supportedJobTypes = options.supportedJobTypes ?? ["MODEL_UNLEARN"];
1105
+ this.qualityGatePolicy = {
1106
+ ...DEFAULT_WHITEBOX_QUALITY_GATE_POLICY,
1107
+ ...options.qualityGatePolicy || {}
1108
+ };
1109
+ this.qualityGateOnFailure = options.qualityGateOnFailure || "fail_job";
1110
+ this.resultClaimMode = options.resultClaimMode || "hash";
1111
+ this.logger = options.logger ?? defaultLogger;
1112
+ }
1113
+ async start() {
1114
+ if (this.running) {
1115
+ return;
1116
+ }
1117
+ this.running = true;
1118
+ this.logger.info("worker started", {
1119
+ projectId: this.options.projectId,
1120
+ adapter: this.adapter.name,
1121
+ claimBatchSize: this.claimBatchSize
1122
+ });
1123
+ this.loopPromise = this.runLoop();
1124
+ }
1125
+ async stop() {
1126
+ this.running = false;
1127
+ if (this.loopPromise) {
1128
+ await this.loopPromise;
1129
+ this.loopPromise = null;
1130
+ }
1131
+ this.logger.info("worker stopped", { projectId: this.options.projectId });
1132
+ }
1133
+ async runOnce() {
1134
+ const jobs = await this.client.claimJobs(this.options.projectId, {
1135
+ limit: this.claimBatchSize,
1136
+ types: this.supportedJobTypes
1137
+ });
1138
+ if (!jobs.length) {
1139
+ return;
1140
+ }
1141
+ for (const job of jobs) {
1142
+ await this.processClaimedJob(job);
1143
+ }
1144
+ }
1145
+ async runLoop() {
1146
+ while (this.running) {
1147
+ try {
1148
+ await this.runOnce();
1149
+ } catch (error) {
1150
+ this.logger.error("worker loop error", {
1151
+ projectId: this.options.projectId,
1152
+ error: safeErrorMessage(error)
1153
+ });
1154
+ }
1155
+ await new Promise((resolve) => setTimeout(resolve, this.pollIntervalMs));
1156
+ }
1157
+ }
1158
+ async processClaimedJob(job) {
1159
+ const claimId = typeof job.lastClaimId === "string" ? job.lastClaimId : void 0;
1160
+ if (!this.supportedJobTypes.includes(job.type)) {
1161
+ this.logger.warn("unsupported job type claimed", {
1162
+ jobId: job.id,
1163
+ type: job.type
1164
+ });
1165
+ await this.client.failJob(this.options.projectId, job.id, {
1166
+ error: {
1167
+ code: "VALIDATION_ERROR",
1168
+ message: `Unsupported job type: ${job.type}`
1169
+ }
1170
+ }, { claimId });
1171
+ return;
1172
+ }
1173
+ const context = {
1174
+ projectId: this.options.projectId,
1175
+ jobId: job.id,
1176
+ correlationId: claimId
1177
+ };
1178
+ let heartbeatTimer = null;
1179
+ const heartbeat = async () => {
1180
+ try {
1181
+ await this.client.heartbeatJob(this.options.projectId, job.id, { claimId });
1182
+ } catch (error) {
1183
+ this.logger.warn("heartbeat failed", {
1184
+ jobId: job.id,
1185
+ error: safeErrorMessage(error)
1186
+ });
1187
+ }
1188
+ };
1189
+ try {
1190
+ const claim = toClaim(job);
1191
+ const config = toConfig(job);
1192
+ const model = resolveModel(config);
1193
+ const target = resolveTarget(claim, config);
1194
+ const plan = resolvePlan(config);
1195
+ const perJobQualityGate = resolvePerJobQualityGatePolicy(this.qualityGatePolicy, config, plan);
1196
+ heartbeatTimer = setInterval(heartbeat, this.heartbeatIntervalMs);
1197
+ await heartbeat();
1198
+ const unlearningOutput = await this.adapter.runUnlearning(
1199
+ { model, target, plan, claim, config },
1200
+ context
1201
+ );
1202
+ assertUnlearningOutput(unlearningOutput);
1203
+ let evaluationOutput;
1204
+ if (this.adapter.runEvaluation) {
1205
+ evaluationOutput = await this.adapter.runEvaluation(
1206
+ {
1207
+ model: unlearningOutput.updatedModel,
1208
+ baselineModel: model,
1209
+ claim,
1210
+ config,
1211
+ probes: config.parameters?.probes || []
1212
+ },
1213
+ context
1214
+ );
1215
+ assertEvaluationOutput(evaluationOutput);
1216
+ }
1217
+ const qualityGate = evaluateWhiteboxQualityGate(
1218
+ unlearningOutput,
1219
+ evaluationOutput,
1220
+ perJobQualityGate.policy
1221
+ );
1222
+ if (!qualityGate.pass && this.qualityGateOnFailure === "fail_job") {
1223
+ throw new Error(`QUALITY_GATE_FAILED: ${qualityGate.reasons.join(" | ")}`);
1224
+ }
1225
+ const finalVerdict = qualityGate.pass ? evaluationOutput?.finalVerdict || "REVIEW" : "REVIEW";
1226
+ const result = {
1227
+ result: {
1228
+ worker: {
1229
+ adapter: this.adapter.name,
1230
+ mode: "whitebox"
1231
+ },
1232
+ claim: buildResultClaim(claim, this.resultClaimMode),
1233
+ plan,
1234
+ unlearning: unlearningOutput,
1235
+ evaluation: evaluationOutput || null,
1236
+ quality_gate: qualityGate,
1237
+ quality_gate_preset: perJobQualityGate.preset,
1238
+ final_verdict: finalVerdict,
1239
+ report_hash: unlearningOutput.modelAfterSha256 || null,
1240
+ privacy: {
1241
+ claim_mode: this.resultClaimMode
1242
+ }
1243
+ }
1244
+ };
1245
+ await this.client.completeJob(this.options.projectId, job.id, result, { claimId });
1246
+ this.logger.info("job completed", {
1247
+ jobId: job.id,
1248
+ type: job.type,
1249
+ adapter: this.adapter.name
1250
+ });
1251
+ } catch (error) {
1252
+ this.logger.error("job failed", {
1253
+ jobId: job.id,
1254
+ type: job.type,
1255
+ error: safeErrorMessage(error)
1256
+ });
1257
+ await this.client.failJob(this.options.projectId, job.id, toJobError(error), { claimId });
1258
+ } finally {
1259
+ if (heartbeatTimer) {
1260
+ clearInterval(heartbeatTimer);
1261
+ }
1262
+ }
1263
+ }
1264
+ };
1265
+
1266
+ // src/native-adapters.ts
1267
+ var import_node_child_process = require("child_process");
1268
+ var DEFAULT_ADAPTER_TIMEOUT_MS = 30 * 60 * 1e3;
1269
+ var LocalCommandTrainingAdapter = class {
1270
+ constructor(options) {
1271
+ this.options = options;
1272
+ this.name = options.name || "local-command-adapter";
1273
+ }
1274
+ async runUnlearning(input, context) {
1275
+ const spec = resolveCommandSpec(this.options.unlearning, input, context);
1276
+ const output = await runJsonCommand(
1277
+ spec,
1278
+ {
1279
+ mode: "unlearning",
1280
+ input,
1281
+ context
1282
+ },
1283
+ this.options.timeoutMs ?? DEFAULT_ADAPTER_TIMEOUT_MS
1284
+ );
1285
+ return output;
1286
+ }
1287
+ async runEvaluation(input, context) {
1288
+ if (!this.options.evaluation) {
1289
+ throw new Error("LocalCommandTrainingAdapter evaluation command is not configured.");
1290
+ }
1291
+ const spec = resolveCommandSpec(this.options.evaluation, input, context);
1292
+ const output = await runJsonCommand(
1293
+ spec,
1294
+ {
1295
+ mode: "evaluation",
1296
+ input,
1297
+ context
1298
+ },
1299
+ this.options.timeoutMs ?? DEFAULT_ADAPTER_TIMEOUT_MS
1300
+ );
1301
+ return output;
1302
+ }
1303
+ };
1304
+ var HttpTrainingAdapter = class {
1305
+ constructor(options) {
1306
+ this.options = options;
1307
+ this.name = options.name || "http-training-adapter";
1308
+ }
1309
+ async runUnlearning(input, context) {
1310
+ const output = await postJson(
1311
+ this.options.unlearningUrl,
1312
+ {
1313
+ mode: "unlearning",
1314
+ input,
1315
+ context
1316
+ },
1317
+ this.options.headers,
1318
+ this.options.timeoutMs ?? DEFAULT_ADAPTER_TIMEOUT_MS
1319
+ );
1320
+ return output;
1321
+ }
1322
+ async runEvaluation(input, context) {
1323
+ if (!this.options.evaluationUrl) {
1324
+ throw new Error("HttpTrainingAdapter evaluationUrl is not configured.");
1325
+ }
1326
+ const output = await postJson(
1327
+ this.options.evaluationUrl,
1328
+ {
1329
+ mode: "evaluation",
1330
+ input,
1331
+ context
1332
+ },
1333
+ this.options.headers,
1334
+ this.options.timeoutMs ?? DEFAULT_ADAPTER_TIMEOUT_MS
1335
+ );
1336
+ return output;
1337
+ }
1338
+ };
1339
+ var createNativeTrainingAdapter = (config) => {
1340
+ if (config.provider === "local_command") {
1341
+ const { provider: _provider2, ...rest2 } = config;
1342
+ return new LocalCommandTrainingAdapter(rest2);
1343
+ }
1344
+ const { provider: _provider, ...rest } = config;
1345
+ return new HttpTrainingAdapter(rest);
1346
+ };
1347
+ function resolveCommandSpec(resolver, input, context) {
1348
+ const resolved = typeof resolver === "function" ? resolver(input, context) : resolver;
1349
+ if (!resolved?.command) {
1350
+ throw new Error("Adapter command is missing.");
1351
+ }
1352
+ return resolved;
1353
+ }
1354
+ async function runJsonCommand(spec, payload, defaultTimeoutMs) {
1355
+ const timeoutMs = spec.timeoutMs ?? defaultTimeoutMs;
1356
+ return new Promise((resolve, reject) => {
1357
+ const child = (0, import_node_child_process.spawn)(spec.command, spec.args || [], {
1358
+ cwd: spec.cwd,
1359
+ env: { ...process.env, ...spec.env || {} },
1360
+ stdio: ["pipe", "pipe", "pipe"]
1361
+ });
1362
+ let stdout = "";
1363
+ let stderr = "";
1364
+ let timedOut = false;
1365
+ let hardKillTimer = null;
1366
+ const timer = setTimeout(() => {
1367
+ timedOut = true;
1368
+ child.kill("SIGTERM");
1369
+ hardKillTimer = setTimeout(() => child.kill("SIGKILL"), 1e3);
1370
+ }, timeoutMs);
1371
+ child.stdout.on("data", (chunk) => {
1372
+ stdout += chunk.toString();
1373
+ });
1374
+ child.stderr.on("data", (chunk) => {
1375
+ stderr += chunk.toString();
1376
+ });
1377
+ child.on("error", (error) => {
1378
+ clearTimeout(timer);
1379
+ if (hardKillTimer) clearTimeout(hardKillTimer);
1380
+ reject(error);
1381
+ });
1382
+ child.on("close", (code) => {
1383
+ clearTimeout(timer);
1384
+ if (hardKillTimer) clearTimeout(hardKillTimer);
1385
+ if (timedOut) {
1386
+ reject(new Error(`Adapter command timed out after ${timeoutMs}ms: ${spec.command}`));
1387
+ return;
1388
+ }
1389
+ if (code !== 0) {
1390
+ reject(
1391
+ new Error(
1392
+ `Adapter command failed (${code}): ${spec.command} ${(spec.args || []).join(" ")}; stderr=${stderr.trim()}`
1393
+ )
1394
+ );
1395
+ return;
1396
+ }
1397
+ const trimmed = stdout.trim();
1398
+ if (!trimmed) {
1399
+ reject(new Error(`Adapter command returned empty stdout: ${spec.command}`));
1400
+ return;
1401
+ }
1402
+ try {
1403
+ resolve(JSON.parse(trimmed));
1404
+ } catch {
1405
+ reject(new Error(`Adapter command output is not valid JSON: ${trimmed.slice(0, 800)}`));
1406
+ }
1407
+ });
1408
+ if (spec.stdinMode !== "none") {
1409
+ try {
1410
+ child.stdin.write(JSON.stringify(payload));
1411
+ } catch (error) {
1412
+ clearTimeout(timer);
1413
+ if (hardKillTimer) clearTimeout(hardKillTimer);
1414
+ reject(error);
1415
+ return;
1416
+ }
1417
+ }
1418
+ child.stdin.end();
1419
+ });
1420
+ }
1421
+ async function postJson(url, body, headers, timeoutMs) {
1422
+ const controller = new AbortController();
1423
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
1424
+ try {
1425
+ const response = await fetch(url, {
1426
+ method: "POST",
1427
+ headers: {
1428
+ "content-type": "application/json",
1429
+ ...headers || {}
1430
+ },
1431
+ body: JSON.stringify(body),
1432
+ signal: controller.signal
1433
+ });
1434
+ const text = await response.text();
1435
+ if (!response.ok) {
1436
+ throw new Error(`HTTP adapter request failed (${response.status}): ${text.slice(0, 800)}`);
1437
+ }
1438
+ if (!text.trim()) {
1439
+ throw new Error(`HTTP adapter returned empty response: ${url}`);
1440
+ }
1441
+ try {
1442
+ return JSON.parse(text);
1443
+ } catch {
1444
+ throw new Error(`HTTP adapter returned non-JSON response: ${text.slice(0, 800)}`);
1445
+ }
1446
+ } finally {
1447
+ clearTimeout(timeout);
1448
+ }
1449
+ }
1450
+
1451
+ // src/deployment-profiles.ts
1452
+ var ENTERPRISE_STRICT_QUALITY_GATE_POLICY = {
1453
+ ...WHITEBOX_QUALITY_GATE_PRESETS.strict
1454
+ };
1455
+ function assertDeploymentCompatibility(options) {
1456
+ const normalizedHost = parseHost(options.apiUrl);
1457
+ if (!normalizedHost) {
1458
+ throw new Error(`Invalid FORG3T_API_URL: ${options.apiUrl}`);
1459
+ }
1460
+ if (options.profile !== "customer_airgapped") {
1461
+ return;
1462
+ }
1463
+ if (options.adapterMode === "http" && !options.allowHttpInAirgapped) {
1464
+ throw new Error(
1465
+ "Air-gapped profile rejects adapterMode=http by default. Use local_command or explicitly set allowHttpInAirgapped=true for trusted internal gateways."
1466
+ );
1467
+ }
1468
+ if (!isHostAllowedForAirgapped(normalizedHost, options.allowedApiHosts || [])) {
1469
+ throw new Error(
1470
+ `Air-gapped profile requires a private/control-plane host. Received ${normalizedHost}. Set allowedApiHosts for private DNS hosts if needed.`
1471
+ );
1472
+ }
1473
+ }
1474
+ function buildWhiteboxWorkerOptions(profile, baseOptions) {
1475
+ const strictMode = profile === "customer_airgapped" || profile === "customer_online";
1476
+ const defaultPolicy = strictMode ? ENTERPRISE_STRICT_QUALITY_GATE_POLICY : DEFAULT_WHITEBOX_QUALITY_GATE_POLICY;
1477
+ const defaultSupportedTypes = ["MODEL_UNLEARN"];
1478
+ return {
1479
+ projectId: baseOptions.projectId,
1480
+ pollIntervalMs: baseOptions.pollIntervalMs ?? 2e3,
1481
+ claimBatchSize: baseOptions.claimBatchSize ?? 1,
1482
+ heartbeatIntervalMs: baseOptions.heartbeatIntervalMs ?? 2e4,
1483
+ supportedJobTypes: baseOptions.supportedJobTypes ?? defaultSupportedTypes,
1484
+ qualityGatePolicy: baseOptions.qualityGatePolicy ?? defaultPolicy,
1485
+ qualityGateOnFailure: baseOptions.qualityGateOnFailure ?? "fail_job",
1486
+ resultClaimMode: baseOptions.resultClaimMode ?? "hash",
1487
+ logger: baseOptions.logger
1488
+ };
1489
+ }
1490
+ function parseHost(apiUrl) {
1491
+ try {
1492
+ const parsed = new URL(apiUrl);
1493
+ return parsed.hostname.toLowerCase();
1494
+ } catch {
1495
+ return null;
1496
+ }
1497
+ }
1498
+ function isHostAllowedForAirgapped(host, allowedApiHosts) {
1499
+ if (isPrivateHost(host)) {
1500
+ return true;
1501
+ }
1502
+ const allowSet = new Set(allowedApiHosts.map((item) => item.trim().toLowerCase()).filter(Boolean));
1503
+ return allowSet.has(host);
1504
+ }
1505
+ function isPrivateHost(host) {
1506
+ if (host === "localhost" || host === "127.0.0.1" || host === "::1") {
1507
+ return true;
1508
+ }
1509
+ if (host.endsWith(".local") || host.endsWith(".internal") || host.endsWith(".lan")) {
1510
+ return true;
1511
+ }
1512
+ const parts = host.split(".");
1513
+ if (parts.length !== 4 || parts.some((part) => Number.isNaN(Number(part)))) {
1514
+ return false;
1515
+ }
1516
+ const octets = parts.map((part) => Number(part));
1517
+ const [a, b] = octets;
1518
+ if (a === 10) return true;
1519
+ if (a === 127) return true;
1520
+ if (a === 192 && b === 168) return true;
1521
+ if (a === 172 && b >= 16 && b <= 31) return true;
1522
+ return false;
1523
+ }
448
1524
  // Annotate the CommonJS export names for ESM import in node:
449
1525
  0 && (module.exports = {
1526
+ AccessLevel,
1527
+ DEFAULT_WHITEBOX_QUALITY_GATE_POLICY,
1528
+ ENTERPRISE_STRICT_QUALITY_GATE_POLICY,
450
1529
  Forg3tApiConnectionError,
451
1530
  Forg3tAuthenticationError,
452
1531
  Forg3tClient,
453
1532
  Forg3tError,
454
1533
  Forg3tNotFoundError,
455
- Forg3tRateLimitError
1534
+ Forg3tNotImplementedError,
1535
+ Forg3tRateLimitError,
1536
+ HttpTrainingAdapter,
1537
+ IntegrationMode,
1538
+ IntegrationType,
1539
+ LocalCommandTrainingAdapter,
1540
+ WHITEBOX_QUALITY_GATE_PRESETS,
1541
+ WHITEBOX_QUALITY_GATE_PRESET_EXPLANATIONS,
1542
+ WhiteboxWorker,
1543
+ assertDeploymentCompatibility,
1544
+ buildModelUnlearningPayload,
1545
+ buildWhiteboxWorkerOptions,
1546
+ createNativeTrainingAdapter,
1547
+ evaluateWhiteboxQualityGate,
1548
+ resolveWhiteboxQualityGatePolicy
456
1549
  });