@stll/anonymize-mcp 2.4.2 → 2.5.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/index.d.mts CHANGED
@@ -102,6 +102,11 @@ declare class PathScope {
102
102
  private constructor();
103
103
  static create(roots: readonly string[]): Promise<PathScope>;
104
104
  readInput({ path, extension, maximumBytes, label }: ReadInputOptions): Promise<ScopedInput>;
105
+ /**
106
+ * Canonicalize an input path, mapping a missing path to a `not_found` surface
107
+ * error instead of a raw fs `ENOENT` so agents get a stable code.
108
+ */
109
+ private canonicalInput;
105
110
  output(path: string, extension: ".docx" | ".pdf" | ".txt"): Promise<ScopedOutput>;
106
111
  }
107
112
  declare const textInput: z.ZodObject<{
package/dist/local.mjs CHANGED
@@ -1,5 +1,8 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { CAPABILITY_MANIFEST } from "@stll/anonymize";
3
+ import { preloadNativeBinding } from "@stll/anonymize/native-runtime";
4
+ import { AnonymizeSurfaceError, classifyToEnvelope } from "@stll/anonymize/agent-surface";
5
+ import { FEEDBACK_KINDS, MAX_FEEDBACK_BODY_CHARS, MAX_FEEDBACK_TITLE_CHARS, buildFeedbackSubmission } from "@stll/anonymize/feedback";
3
6
  import { DOCX_ARCHIVE_MAX_BYTES, DOCX_COVERAGE_MODES, anonymizeDocx, extractDocxText, restoreDocxText } from "@stll/anonymize-docx";
4
7
  import { PDF_DOCUMENT_MAX_BYTES, anonymizePdfRaster, renderPdfWithPopplerTesseract } from "@stll/anonymize-pdf";
5
8
  import * as nativeNode from "@stll/anonymize/native-node";
@@ -7,6 +10,9 @@ import { constants, link, lstat, open, readdir, realpath, rename, stat, unlink }
7
10
  import { createHash, randomUUID } from "node:crypto";
8
11
  import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
9
12
  import * as z from "zod/v4";
13
+ //#region package.json
14
+ var version = "2.5.0";
15
+ //#endregion
10
16
  //#region src/durable-sessions.ts
11
17
  const SESSION_ARCHIVE_KEY_BYTES = 32;
12
18
  const SESSION_ARCHIVE_MAX_BYTES = 16777273;
@@ -355,6 +361,33 @@ const PATH_MAX_CHARACTERS = 32768;
355
361
  const SESSION_MAX_COUNT = 256;
356
362
  const SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;
357
363
  const READ_CHUNK_BYTES = 64 * 1024;
364
+ /** Build an agent-surface error carrying a stable code, message, and hint. */
365
+ const surfaceError = (code, message, hint, retryable = false) => new AnonymizeSurfaceError(code, message, {
366
+ hint,
367
+ retryable
368
+ });
369
+ const nodeErrorCode = (error) => typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
370
+ /**
371
+ * The local PDF provider collapses a missing/failed pdftoppm or tesseract into a
372
+ * `PdfLocalProviderError` with code `executable-failed`; treat it as a missing
373
+ * dependency so agents get an actionable install hint instead of a generic
374
+ * internal error. Duck-typed to avoid importing the provider's internals.
375
+ */
376
+ const isPdfToolchainUnavailable = (error) => typeof error === "object" && error !== null && error.name === "PdfLocalProviderError" && error.code === "executable-failed";
377
+ /**
378
+ * The docx package refuses under requireFull coverage with a
379
+ * `DocxAnonymizationError` (code `incomplete-coverage`). Map it to a
380
+ * validation_error so the agent gets the actionable allowPartialCoverage hint
381
+ * instead of a generic internal_error; pass anything else through unchanged.
382
+ * Duck-typed to avoid importing the docx error class.
383
+ */
384
+ const mapDocxCoverageError = (error) => {
385
+ if (typeof error === "object" && error !== null && error.name === "DocxAnonymizationError" && error.code === "incomplete-coverage") return new AnonymizeSurfaceError("validation_error", error.message, {
386
+ hint: "Re-run with allowPartialCoverage: true to accept partial coverage.",
387
+ cause: error
388
+ });
389
+ return error;
390
+ };
358
391
  const observedAtEpochSeconds = () => {
359
392
  const seconds = Math.floor(Date.now() / 1e3);
360
393
  if (seconds < 0 || seconds > 4294967295) throw new Error("The current time is outside the supported session range");
@@ -414,7 +447,7 @@ const readHandleBounded = async (handle, maximumBytes, label) => {
414
447
  const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null);
415
448
  if (bytesRead === 0) return Buffer.concat(chunks, total);
416
449
  total += bytesRead;
417
- if (total > maximumBytes) throw new Error(`${label} inputs must not exceed ${maximumBytes} bytes`);
450
+ if (total > maximumBytes) throw surfaceError("validation_error", `${label} inputs must not exceed ${maximumBytes} bytes`, "Split or shrink the input below the size limit and retry.");
418
451
  chunks.push(chunk.subarray(0, bytesRead));
419
452
  }
420
453
  };
@@ -445,19 +478,20 @@ var PathScope = class PathScope {
445
478
  return new PathScope([...new Set(canonical)]);
446
479
  }
447
480
  async readInput({ path, extension, maximumBytes, label }) {
448
- if (!isAbsolute(path) || extname(path).toLowerCase() !== extension) throw new Error(`Input must be an absolute ${extension} path`);
449
- const initiallyCanonical = await realpath(path);
450
- if (!this.#roots.some((root) => inside(root, initiallyCanonical))) throw new Error("Input is outside the configured roots");
451
- if (!(await lstat(path)).isFile()) throw new Error("Input must be a regular file");
481
+ if (!isAbsolute(path)) throw surfaceError("validation_error", `Input must be an absolute ${extension} path`, "Pass an absolute path to an existing file inside a configured --root.");
482
+ if (extname(path).toLowerCase() !== extension) throw surfaceError("unsupported_format", `Input must be an absolute ${extension} path`, `Provide a file with the ${extension} extension.`);
483
+ const initiallyCanonical = await this.canonicalInput(path);
484
+ if (!this.#roots.some((root) => inside(root, initiallyCanonical))) throw surfaceError("path_not_allowed", "Input is outside the configured roots", "Move the input under a configured --root, or add its directory with --root.");
485
+ if (!(await lstat(path)).isFile()) throw surfaceError("validation_error", "Input must be a regular file", "Point at a regular file, not a directory, symlink, or special file.");
452
486
  const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
453
487
  try {
454
488
  const openedMetadata = await handle.stat();
455
- if (!openedMetadata.isFile()) throw new Error("Input must be a regular file");
489
+ if (!openedMetadata.isFile()) throw surfaceError("validation_error", "Input must be a regular file", "Point at a regular file, not a directory, symlink, or special file.");
456
490
  const canonical = await realpath(path);
457
- if (!this.#roots.some((root) => inside(root, canonical))) throw new Error("Input is outside the configured roots");
491
+ if (!this.#roots.some((root) => inside(root, canonical))) throw surfaceError("path_not_allowed", "Input is outside the configured roots", "Move the input under a configured --root, or add its directory with --root.");
458
492
  const currentMetadata = await stat(canonical);
459
493
  if (currentMetadata.dev !== openedMetadata.dev || currentMetadata.ino !== openedMetadata.ino) throw new Error("Input changed while it was being validated");
460
- if (openedMetadata.size > maximumBytes) throw new Error(`${label} inputs must not exceed ${maximumBytes} bytes`);
494
+ if (openedMetadata.size > maximumBytes) throw surfaceError("validation_error", `${label} inputs must not exceed ${maximumBytes} bytes`, "Split or shrink the input below the size limit and retry.");
461
495
  return {
462
496
  bytes: await readHandleBounded(handle, maximumBytes, label),
463
497
  path: canonical
@@ -466,11 +500,30 @@ var PathScope = class PathScope {
466
500
  await handle.close();
467
501
  }
468
502
  }
503
+ /**
504
+ * Canonicalize an input path, mapping a missing path to a `not_found` surface
505
+ * error instead of a raw fs `ENOENT` so agents get a stable code.
506
+ */
507
+ async canonicalInput(path) {
508
+ try {
509
+ return await realpath(path);
510
+ } catch (error) {
511
+ if (nodeErrorCode(error) === "ENOENT") throw surfaceError("not_found", "Input path does not exist", "Create the file first, or pass an existing path inside a configured --root.");
512
+ throw error;
513
+ }
514
+ }
469
515
  async output(path, extension) {
470
- if (!isAbsolute(path) || extname(path).toLowerCase() !== extension) throw new Error(`Output must be an absolute ${extension} path`);
516
+ if (!isAbsolute(path)) throw surfaceError("validation_error", `Output must be an absolute ${extension} path`, "Pass an absolute output path inside a configured --root.");
517
+ if (extname(path).toLowerCase() !== extension) throw surfaceError("unsupported_format", `Output must be an absolute ${extension} path`, `Name the output with the ${extension} extension.`);
471
518
  const normalized = resolve(path);
472
- const parent = await realpath(dirname(normalized));
473
- if (!this.#roots.some((root) => inside(root, parent))) throw new Error("Output is outside the configured roots");
519
+ let parent;
520
+ try {
521
+ parent = await realpath(dirname(normalized));
522
+ } catch (error) {
523
+ if (nodeErrorCode(error) === "ENOENT") throw surfaceError("not_found", "Output directory does not exist", "Create the output directory first, inside a configured --root.");
524
+ throw error;
525
+ }
526
+ if (!this.#roots.some((root) => inside(root, parent))) throw surfaceError("path_not_allowed", "Output is outside the configured roots", "Choose an output directory under a configured --root.");
474
527
  const parentMetadata = await stat(parent);
475
528
  try {
476
529
  await lstat(normalized);
@@ -482,7 +535,7 @@ var PathScope = class PathScope {
482
535
  });
483
536
  throw new Error("Output availability could not be verified", { cause: error });
484
537
  }
485
- throw new Error("Output already exists; overwriting is not supported");
538
+ throw surfaceError("output_exists", "Output already exists; overwriting is not supported", "Pick a new output path; anonymize never overwrites an existing file.");
486
539
  }
487
540
  };
488
541
  const nativeNodeSurface = nativeNode;
@@ -538,7 +591,10 @@ const decodeUtf8 = (bytes, label) => {
538
591
  try {
539
592
  return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
540
593
  } catch (error) {
541
- throw new Error(`${label} must contain valid UTF-8`, { cause: error });
594
+ throw new AnonymizeSurfaceError("validation_error", `${label} must contain valid UTF-8`, {
595
+ hint: "Provide UTF-8 encoded text; re-encode the file and retry.",
596
+ cause: error
597
+ });
542
598
  }
543
599
  };
544
600
  const applyTextReplacements = (text, replacements) => {
@@ -559,7 +615,7 @@ const isUtf16Boundary = (text, offset) => {
559
615
  return !(before >= 55296 && before <= 56319 && after >= 56320 && after <= 57343);
560
616
  };
561
617
  const assertDifferentPaths = (input, output) => {
562
- if (input === output) throw new Error("Input and output paths must differ");
618
+ if (input === output) throw surfaceError("validation_error", "Input and output paths must differ", "Choose a distinct output path so the input is never overwritten.");
563
619
  };
564
620
  const textInput = z.object({
565
621
  inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),
@@ -632,6 +688,7 @@ var LocalAnonymizeService = class {
632
688
  if (this.#state !== "open") throw new Error("MCP anonymize service is closing or closed");
633
689
  this.#activeOperations += 1;
634
690
  try {
691
+ await preloadNativeBinding();
635
692
  return await operation();
636
693
  } finally {
637
694
  this.#activeOperations -= 1;
@@ -645,7 +702,7 @@ var LocalAnonymizeService = class {
645
702
  if (this.#durableSessions !== void 0 && language !== void 0) throw new Error("Durable sessions use the full all-language pipeline; omit language");
646
703
  const existing = this.#sessions.get(sessionId);
647
704
  if (existing !== void 0) {
648
- if (language !== void 0 && existing.language !== language) throw new Error("A session cannot change language");
705
+ if (language !== void 0 && existing.language !== language) throw surfaceError("validation_error", "A session cannot change language", "Use a new session id for a different language, or drop the language override.");
649
706
  if (existing.status === "initializing") throw new Error("The requested session is still initializing");
650
707
  if (existing.status === "busy") throw new Error("The requested session is handling another operation");
651
708
  const checkpoint = existing.session.toPlaintextJson();
@@ -669,7 +726,7 @@ var LocalAnonymizeService = class {
669
726
  let session;
670
727
  if (stored === void 0) session = pipeline.createRedactionSession(sessionId);
671
728
  else {
672
- if (durableSessions === void 0) throw new Error("Durable session storage is unavailable");
729
+ if (durableSessions === void 0) throw surfaceError("session_unavailable", "Durable session storage is unavailable", "Start the server with --session-dir and --key-file to enable restores.");
673
730
  try {
674
731
  session = durableSessions.restore({
675
732
  archive: stored.bytes,
@@ -678,7 +735,10 @@ var LocalAnonymizeService = class {
678
735
  restorer: pipeline
679
736
  });
680
737
  } catch (error) {
681
- throw new Error("The requested durable session is unavailable", { cause: error });
738
+ throw new AnonymizeSurfaceError("session_unavailable", "The requested durable session is unavailable", {
739
+ hint: "Confirm the session id and key file match the archive that created it.",
740
+ cause: error
741
+ });
682
742
  }
683
743
  }
684
744
  const entry = {
@@ -744,14 +804,17 @@ var LocalAnonymizeService = class {
744
804
  };
745
805
  this.#sessions.set(sessionId, entry);
746
806
  } catch (error) {
747
- throw new Error("The requested durable session is unavailable", { cause: error });
807
+ throw new AnonymizeSurfaceError("session_unavailable", "The requested durable session is unavailable", {
808
+ hint: "Confirm the session id and key file match the archive that created it.",
809
+ cause: error
810
+ });
748
811
  }
749
812
  } finally {
750
813
  this.#sessionInitializations.delete(sessionId);
751
814
  }
752
815
  }
753
- if (entry === void 0) throw new Error("The requested session is unavailable");
754
- if (entry.status !== "ready") throw new Error("The requested in-memory session is unavailable");
816
+ if (entry === void 0) throw surfaceError("session_unavailable", "The requested session is unavailable", "Anonymize with this session id first, or start the server with a durable session store.");
817
+ if (entry.status !== "ready") throw surfaceError("session_unavailable", "The requested in-memory session is unavailable", "Wait for the prior operation on this session to finish, then retry.", true);
755
818
  entry.status = "busy";
756
819
  return {
757
820
  entry,
@@ -793,13 +856,22 @@ var LocalAnonymizeService = class {
793
856
  const destination = await this.#scope.output(input.outputPath, ".pdf");
794
857
  assertDifferentPaths(source.path, destination.path);
795
858
  const pipeline = nativeNodeSurface.getDefaultNativePipeline(input.detectionLanguage === void 0 ? {} : { language: input.detectionLanguage });
796
- const observed = await renderPdfWithPopplerTesseract({
797
- document: source.bytes,
798
- ocrLanguage: input.ocrLanguage,
799
- dpi: input.dpi,
800
- timeoutMs: input.timeoutMs,
801
- ...this.#pdfProvider
802
- });
859
+ let observed;
860
+ try {
861
+ observed = await renderPdfWithPopplerTesseract({
862
+ document: source.bytes,
863
+ ocrLanguage: input.ocrLanguage,
864
+ dpi: input.dpi,
865
+ timeoutMs: input.timeoutMs,
866
+ ...this.#pdfProvider
867
+ });
868
+ } catch (error) {
869
+ if (isPdfToolchainUnavailable(error)) throw new AnonymizeSurfaceError("dependency_missing", "The local PDF toolchain is unavailable", {
870
+ hint: "Install Poppler (pdftoppm) and Tesseract on PATH, or pass --pdftoppm/--tesseract.",
871
+ cause: error
872
+ });
873
+ throw error;
874
+ }
803
875
  const anonymized = anonymizePdfRaster({
804
876
  document: source.bytes,
805
877
  pipeline,
@@ -977,7 +1049,7 @@ var LocalAnonymizeService = class {
977
1049
  };
978
1050
  } catch (error) {
979
1051
  await this.#rollbackSession(lease);
980
- throw error;
1052
+ throw mapDocxCoverageError(error);
981
1053
  }
982
1054
  }
983
1055
  async restoreDocx(input) {
@@ -999,7 +1071,7 @@ var LocalAnonymizeService = class {
999
1071
  session: lease.entry.session,
1000
1072
  expectedSessionId: input.sessionId
1001
1073
  });
1002
- if (result.coverage.status === "partial" && !input.allowPartialCoverage) throw new Error("DOCX restoration has partial coverage; set allowPartialCoverage to publish it");
1074
+ if (result.coverage.status === "partial" && !input.allowPartialCoverage) throw surfaceError("validation_error", "DOCX restoration has partial coverage; set allowPartialCoverage to publish it", "Re-run with allowPartialCoverage: true to accept partial restoration.");
1003
1075
  await destination.write(result.document);
1004
1076
  this.#commitSession(lease);
1005
1077
  return {
@@ -1052,9 +1124,78 @@ const MCP_TOOL_NAMES = [
1052
1124
  "capabilities",
1053
1125
  "inspect_docx_file",
1054
1126
  "restore_docx_file",
1055
- "restore_text_file"
1127
+ "restore_text_file",
1128
+ "send_feedback"
1056
1129
  ];
1057
- const capabilitiesResult = (service) => {
1130
+ /**
1131
+ * Map the external-detection failure taxonomy onto the shared agent-surface
1132
+ * codes. The specific failure identity is preserved in the envelope `message`;
1133
+ * `code` gives the agent the coarse, branchable class.
1134
+ */
1135
+ const EXTERNAL_DETECTION_ENVELOPE = {
1136
+ EXTERNAL_DETECTION_BATCH_REJECTED: {
1137
+ code: "validation_error",
1138
+ hint: "Fix the ExternalDetectionBatch v1 sidecar to match the schema and retry.",
1139
+ retryable: false
1140
+ },
1141
+ EXTERNAL_DETECTION_DOCUMENT_REJECTED: {
1142
+ code: "validation_error",
1143
+ hint: "Align the sidecar's document metadata with the input, then retry.",
1144
+ retryable: false
1145
+ },
1146
+ EXTERNAL_DETECTION_INPUT_REJECTED: {
1147
+ code: "validation_error",
1148
+ hint: "Use distinct absolute paths inside a configured --root for input, sidecar, and output.",
1149
+ retryable: false
1150
+ },
1151
+ EXTERNAL_DETECTION_OPERATION_FAILED: {
1152
+ code: "internal_error",
1153
+ hint: "Retry; if it persists, file it with the send_feedback tool.",
1154
+ retryable: true
1155
+ },
1156
+ EXTERNAL_DETECTION_SESSION_REJECTED: {
1157
+ code: "session_unavailable",
1158
+ hint: "Use a fresh session id, or confirm the session store and key file match.",
1159
+ retryable: false
1160
+ }
1161
+ };
1162
+ const toEnvelope = (error) => {
1163
+ if (error instanceof ExternalDetectionAuditError) {
1164
+ const mapped = EXTERNAL_DETECTION_ENVELOPE[error.code];
1165
+ return { error: {
1166
+ code: mapped.code,
1167
+ message: error.message,
1168
+ hint: mapped.hint,
1169
+ retryable: mapped.retryable
1170
+ } };
1171
+ }
1172
+ return classifyToEnvelope(error);
1173
+ };
1174
+ const errorResult = (error) => {
1175
+ const envelope = toEnvelope(error);
1176
+ return {
1177
+ isError: true,
1178
+ content: [{
1179
+ type: "text",
1180
+ text: JSON.stringify(envelope)
1181
+ }],
1182
+ structuredContent: { ...envelope }
1183
+ };
1184
+ };
1185
+ /**
1186
+ * Run a tool body, rendering any thrown error as the structured envelope so
1187
+ * every tool fails the same, agent-legible way instead of surfacing a raw
1188
+ * protocol error.
1189
+ */
1190
+ const guard = async (produce) => {
1191
+ try {
1192
+ return await produce();
1193
+ } catch (error) {
1194
+ return errorResult(error);
1195
+ }
1196
+ };
1197
+ const capabilitiesResult = async (service) => {
1198
+ await preloadNativeBinding();
1058
1199
  const value = {
1059
1200
  capabilityManifest: CAPABILITY_MANIFEST,
1060
1201
  runtimeVersion: nativeNodeSurface.native_package_version(),
@@ -1081,26 +1222,42 @@ const capabilitiesResult = (service) => {
1081
1222
  structuredContent: value
1082
1223
  };
1083
1224
  };
1084
- const externalDetectionErrorResult = (error) => {
1085
- const failure = externalDetectionFailure(error, EXTERNAL_DETECTION_FAILURES.operationFailed);
1225
+ const feedbackInput = z.object({
1226
+ kind: z.enum(FEEDBACK_KINDS),
1227
+ title: z.string().min(1).max(MAX_FEEDBACK_TITLE_CHARS),
1228
+ body: z.string().min(1).max(MAX_FEEDBACK_BODY_CHARS)
1229
+ });
1230
+ const feedbackResult = (input) => {
1231
+ const submission = buildFeedbackSubmission(input);
1086
1232
  const value = {
1087
- errorCode: failure.code,
1088
- message: failure.message
1233
+ channel: "github",
1234
+ redactions: submission.redactions,
1235
+ title: submission.title,
1236
+ sanitizedBody: submission.sanitizedBody,
1237
+ issueUrl: submission.issueUrl,
1238
+ ghCommand: submission.ghCommand,
1239
+ note: "Nothing was sent. Review the sanitized content, then open the URL (or run the gh command) to submit the issue under your own GitHub account."
1089
1240
  };
1090
1241
  return {
1091
- isError: true,
1092
1242
  content: [{
1093
1243
  type: "text",
1094
1244
  text: JSON.stringify(value)
1095
1245
  }],
1096
- structuredContent: value
1246
+ structuredContent: { ...value }
1097
1247
  };
1098
1248
  };
1249
+ const MCP_INSTRUCTIONS = `stella-anonymize redacts PII in local text, DOCX, and PDF files. Every tool reads and writes local paths only, inside the directories passed as --root; it never returns document text or session mappings, and it never overwrites, so outputs must be new paths.
1250
+
1251
+ Errors: a failed tool returns a single text content of {"error":{"code","message","hint","retryable"}} with isError set. Branch on code (validation_error, path_not_allowed, not_found, unsupported_format, output_exists, session_unavailable, dependency_missing, internal_error); hint states the next step. Nothing here is destructive: there is no delete and existing files are never overwritten, so no confirm step is needed.
1252
+
1253
+ Sessions: reversible replace mode uses a session; a restore needs the same session id, plus a durable store (server started with --session-dir and --key-file) to survive a restart.
1254
+
1255
+ Hit a bug or a gap? Use send_feedback: it sanitizes your text locally and returns a prefilled GitHub issue URL you open and submit yourself. It sends nothing over the network.`;
1099
1256
  const createAnonymizeMcpServer = (service) => {
1100
1257
  const server = new McpServer({
1101
1258
  name: "stella-anonymize-local",
1102
- version: nativeNodeSurface.native_package_version()
1103
- }, { instructions: "All tools accept local paths only. Never request or return document contents or session mappings. Outputs must be new explicit paths inside configured roots." });
1259
+ version
1260
+ }, { instructions: MCP_INSTRUCTIONS });
1104
1261
  server.registerTool("capabilities", {
1105
1262
  description: "Return the public runtime capability manifest and MCP surface metadata.",
1106
1263
  inputSchema: z.object({}),
@@ -1109,7 +1266,7 @@ const createAnonymizeMcpServer = (service) => {
1109
1266
  destructiveHint: false,
1110
1267
  idempotentHint: true
1111
1268
  }
1112
- }, async () => capabilitiesResult(service));
1269
+ }, async () => guard(() => capabilitiesResult(service)));
1113
1270
  server.registerTool("anonymize_text_file", {
1114
1271
  description: "Anonymize a local UTF-8 text file into a new local file.",
1115
1272
  inputSchema: textInput,
@@ -1117,7 +1274,7 @@ const createAnonymizeMcpServer = (service) => {
1117
1274
  destructiveHint: false,
1118
1275
  idempotentHint: false
1119
1276
  }
1120
- }, async (input) => result(await service.anonymizeText(input)));
1277
+ }, async (input) => guard(async () => result(await service.anonymizeText(input))));
1121
1278
  server.registerTool("restore_text_file", {
1122
1279
  description: "Restore a text file using the configured session store.",
1123
1280
  inputSchema: restoreInput,
@@ -1125,7 +1282,7 @@ const createAnonymizeMcpServer = (service) => {
1125
1282
  destructiveHint: false,
1126
1283
  idempotentHint: false
1127
1284
  }
1128
- }, async (input) => result(await service.restoreText(input)));
1285
+ }, async (input) => guard(async () => result(await service.restoreText(input))));
1129
1286
  server.registerTool("anonymize_text_file_with_external_detections", {
1130
1287
  description: "Anonymize a local UTF-8 text file with a provider-neutral ExternalDetectionBatch v1 JSON sidecar into a new local file.",
1131
1288
  inputSchema: externalDetectionTextInput,
@@ -1133,13 +1290,7 @@ const createAnonymizeMcpServer = (service) => {
1133
1290
  destructiveHint: false,
1134
1291
  idempotentHint: false
1135
1292
  }
1136
- }, async (input) => {
1137
- try {
1138
- return result(await service.anonymizeTextWithExternalDetections(input));
1139
- } catch (error) {
1140
- return externalDetectionErrorResult(error);
1141
- }
1142
- });
1293
+ }, async (input) => guard(async () => result(await service.anonymizeTextWithExternalDetections(input))));
1143
1294
  server.registerTool("anonymize_docx_file", {
1144
1295
  description: "Structure-preservingly anonymize a local DOCX into a new local DOCX.",
1145
1296
  inputSchema: docxInput,
@@ -1147,7 +1298,7 @@ const createAnonymizeMcpServer = (service) => {
1147
1298
  destructiveHint: false,
1148
1299
  idempotentHint: false
1149
1300
  }
1150
- }, async (input) => result(await service.anonymizeDocx(input)));
1301
+ }, async (input) => guard(async () => result(await service.anonymizeDocx(input))));
1151
1302
  server.registerTool("anonymize_pdf_file", {
1152
1303
  description: "Destructively raster-anonymize a local PDF into a fresh image-only PDF. Returns aggregate verification only; it does not claim perfect OCR or detector recall.",
1153
1304
  inputSchema: pdfInput,
@@ -1155,7 +1306,7 @@ const createAnonymizeMcpServer = (service) => {
1155
1306
  destructiveHint: false,
1156
1307
  idempotentHint: false
1157
1308
  }
1158
- }, async (input) => result(await service.anonymizePdf(input)));
1309
+ }, async (input) => guard(async () => result(await service.anonymizePdf(input))));
1159
1310
  server.registerTool("restore_docx_file", {
1160
1311
  description: "Restore a DOCX using the configured session store.",
1161
1312
  inputSchema: docxRestoreInput,
@@ -1163,7 +1314,7 @@ const createAnonymizeMcpServer = (service) => {
1163
1314
  destructiveHint: false,
1164
1315
  idempotentHint: false
1165
1316
  }
1166
- }, async (input) => result(await service.restoreDocx(input)));
1317
+ }, async (input) => guard(async () => result(await service.restoreDocx(input))));
1167
1318
  server.registerTool("inspect_docx_file", {
1168
1319
  description: "Return only aggregate DOCX coverage and block counts; never document text.",
1169
1320
  inputSchema: z.object({ inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS) }),
@@ -1172,7 +1323,17 @@ const createAnonymizeMcpServer = (service) => {
1172
1323
  destructiveHint: false,
1173
1324
  idempotentHint: true
1174
1325
  }
1175
- }, async ({ inputPath }) => result(await service.inspectDocx(inputPath)));
1326
+ }, async ({ inputPath }) => guard(async () => result(await service.inspectDocx(inputPath))));
1327
+ server.registerTool("send_feedback", {
1328
+ description: "File a bug, feature request, or docs issue with the stella-anonymize maintainers. Sanitizes the title and body locally (emails, ids, secrets, URLs, IPs are redacted) and returns a prefilled GitHub new-issue URL and a gh command that you open and submit under your own account. It sends nothing over the network and publishes nothing on its own. Never include document text, client names, ids, or secrets; describe the problem, steps, and expected vs actual result.",
1329
+ inputSchema: feedbackInput,
1330
+ annotations: {
1331
+ readOnlyHint: true,
1332
+ destructiveHint: false,
1333
+ idempotentHint: true,
1334
+ openWorldHint: false
1335
+ }
1336
+ }, async (input) => guard(() => feedbackResult(input)));
1176
1337
  return server;
1177
1338
  };
1178
1339
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"local.mjs","names":["SESSION_ID","READ_CHUNK_BYTES","sameFile","fsConstants","readHandleBounded","#directory","#faultInjector","#key","#lockHandle","#validateInventory","#syncDirectory","#assertOpen","#closePromise","#state","#mutationTail","#withMutation","#load","#path","#assertDirectory","#save","#inject","#delete","#roots","fsConstants","#scope","#sessionInitializations","#sessions","#durableSessions","#faults","#pdfProvider","#closePromise","#state","#activeOperations","#operationsDrained","#runOperation","#session","#rollbackSession","#persistSession","#readSession","#anonymizeText","#serializePdfOperation","#anonymizePdf","#pdfOperationTail","#commitSession","#restoreText","#anonymizeTextWithExternalDetections","#anonymizeDocx","#restoreDocx","#inspectDocx"],"sources":["../src/durable-sessions.ts","../src/local.ts"],"sourcesContent":["import { createHash, randomUUID } from \"node:crypto\";\nimport {\n constants as fsConstants,\n lstat,\n open,\n readdir,\n realpath,\n rename,\n stat,\n unlink,\n} from \"node:fs/promises\";\nimport type { FileHandle } from \"node:fs/promises\";\nimport { isAbsolute, join, resolve } from \"node:path\";\n\nexport const SESSION_ARCHIVE_KEY_BYTES = 32;\nexport const SESSION_ARCHIVE_MAX_BYTES = 16 * 1024 * 1024 + 57;\nexport const SESSION_ARCHIVE_MAX_COUNT = 256;\nexport const SESSION_ARCHIVE_TOTAL_MAX_BYTES = 256 * 1024 * 1024;\n\nconst ARCHIVE_NAME = /^[a-f0-9]{64}\\.stlasess$/u;\nconst SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;\nconst STAGING_NAME =\n /^[a-f0-9]{64}\\.stlasess\\.tmp\\.[0-9a-f]{8}-[0-9a-f-]{27}$/u;\nconst READ_CHUNK_BYTES = 64 * 1024;\nconst LOCK_FILE_NAME = \".stella-session.lock\";\n\nexport const DURABLE_SESSION_FAULT_POINTS = {\n beforeDirectoryFsync: \"before-directory-fsync\",\n beforeRename: \"before-rename\",\n beforeStagingFsync: \"before-staging-fsync\",\n beforeStagingWrite: \"before-staging-write\",\n} as const;\n\nexport type DurableSessionFaultPoint =\n (typeof DURABLE_SESSION_FAULT_POINTS)[keyof typeof DURABLE_SESSION_FAULT_POINTS];\n\ntype FileIdentity = {\n dev: number;\n ino: number;\n};\n\ntype DirectoryIdentity = FileIdentity & {\n path: string;\n};\n\ntype SessionInventory = {\n archiveCount: number;\n totalBytes: number;\n};\n\nconst sameFile = (left: FileIdentity, right: FileIdentity): boolean =>\n left.dev === right.dev && left.ino === right.ino;\n\nconst assertOwner = (uid: number, label: string): void => {\n if (typeof process.getuid === \"function\" && uid !== process.getuid()) {\n throw new Error(`${label} must be owned by the current user`);\n }\n};\n\nconst assertPrivateMode = (mode: number, label: string): void => {\n if ((mode & 0o077) !== 0) {\n throw new Error(`${label} must not grant group or other permissions`);\n }\n};\n\nconst assertPosixDurabilitySupport = (): void => {\n if (\n (process.platform !== \"darwin\" && process.platform !== \"linux\") ||\n typeof process.getuid !== \"function\" ||\n typeof fsConstants.O_NOFOLLOW !== \"number\" ||\n fsConstants.O_NOFOLLOW === 0 ||\n typeof fsConstants.O_DIRECTORY !== \"number\" ||\n fsConstants.O_DIRECTORY === 0\n ) {\n throw new Error(\n \"Encrypted durable MCP sessions require supported POSIX owner, nofollow, directory-fsync, and advisory-lock semantics on macOS or Linux\",\n );\n }\n};\n\nconst acquireDirectoryLock = async (\n directoryPath: string,\n): Promise<FileHandle> => {\n const path = join(directoryPath, LOCK_FILE_NAME);\n const handle = await open(\n path,\n fsConstants.O_RDWR |\n fsConstants.O_CREAT |\n fsConstants.O_NOFOLLOW |\n fsConstants.O_NONBLOCK,\n 0o600,\n );\n try {\n const metadata = await handle.stat();\n if (!metadata.isFile()) {\n throw new Error(\"MCP session lock must be a regular file\");\n }\n assertOwner(metadata.uid, \"MCP session lock\");\n assertPrivateMode(metadata.mode, \"MCP session lock\");\n // Loaded lazily because Bun is used as a repository test/build tool but\n // cannot safely load this Node native addon. The shipped MCP runtime is\n // Node; Node integration tests exercise this path.\n const { tryLock } = await import(\"fs-native-extensions\");\n if (!tryLock(handle.fd)) {\n throw new Error(\n \"MCP session directory is already locked by another server\",\n );\n }\n return handle;\n } catch (error) {\n await handle.close().catch(() => undefined);\n throw error;\n }\n};\n\nconst canonicalAbsolutePath = async (\n path: string,\n label: string,\n): Promise<string> => {\n if (!isAbsolute(path)) {\n throw new Error(`${label} must be an absolute path`);\n }\n const normalized = resolve(path);\n const canonical = await realpath(normalized);\n if (canonical !== normalized) {\n throw new Error(`${label} must not contain symbolic links`);\n }\n return canonical;\n};\n\nconst readHandleBounded = async (\n handle: Pick<Awaited<ReturnType<typeof open>>, \"read\">,\n maximumBytes: number,\n): Promise<Uint8Array> => {\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const remaining = maximumBytes - total;\n const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, remaining + 1));\n const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null);\n if (bytesRead === 0) {\n return Buffer.concat(chunks, total);\n }\n total += bytesRead;\n if (total > maximumBytes) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n chunks.push(chunk.subarray(0, bytesRead));\n }\n};\n\nconst readKey = async (path: string): Promise<Uint8Array> => {\n const canonical = await canonicalAbsolutePath(path, \"MCP session key file\");\n const linkedMetadata = await lstat(canonical);\n if (!linkedMetadata.isFile() || linkedMetadata.isSymbolicLink()) {\n throw new Error(\"MCP session key file must be a regular file\");\n }\n assertOwner(linkedMetadata.uid, \"MCP session key file\");\n assertPrivateMode(linkedMetadata.mode, \"MCP session key file\");\n if (linkedMetadata.size !== SESSION_ARCHIVE_KEY_BYTES) {\n throw new Error(\n `MCP session key file must contain exactly ${SESSION_ARCHIVE_KEY_BYTES} raw bytes`,\n );\n }\n const handle = await open(\n canonical,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n let keyBuffer: Buffer | undefined;\n try {\n const openedMetadata = await handle.stat();\n if (\n !openedMetadata.isFile() ||\n !sameFile(linkedMetadata, openedMetadata) ||\n openedMetadata.size !== SESSION_ARCHIVE_KEY_BYTES\n ) {\n throw new Error(\"MCP session key file changed while it was validated\");\n }\n keyBuffer = Buffer.alloc(SESSION_ARCHIVE_KEY_BYTES + 1);\n let keyLength = 0;\n for (;;) {\n const { bytesRead } = await handle.read(\n keyBuffer,\n keyLength,\n keyBuffer.byteLength - keyLength,\n null,\n );\n if (bytesRead === 0 || keyLength + bytesRead === keyBuffer.byteLength) {\n keyLength += bytesRead;\n break;\n }\n keyLength += bytesRead;\n }\n if (keyLength !== SESSION_ARCHIVE_KEY_BYTES) {\n keyBuffer.fill(0);\n throw new Error(\n `MCP session key file must contain exactly ${SESSION_ARCHIVE_KEY_BYTES} raw bytes`,\n );\n }\n const currentMetadata = await stat(canonical);\n if (!sameFile(openedMetadata, currentMetadata)) {\n keyBuffer.fill(0);\n throw new Error(\"MCP session key file changed while it was read\");\n }\n const key = new Uint8Array(SESSION_ARCHIVE_KEY_BYTES);\n key.set(keyBuffer.subarray(0, SESSION_ARCHIVE_KEY_BYTES));\n keyBuffer.fill(0);\n return key;\n } finally {\n keyBuffer?.fill(0);\n await handle.close();\n }\n};\n\nconst archiveName = (sessionId: string): string =>\n `${createHash(\"sha256\").update(sessionId, \"utf8\").digest(\"hex\")}.stlasess`;\n\nconst assertSessionId = (sessionId: string): void => {\n if (!SESSION_ID.test(sessionId)) {\n throw new Error(\"MCP session ID is invalid\");\n }\n};\n\nexport type DurableSessionStoreOptions = {\n faultInjector?: (point: DurableSessionFaultPoint) => Promise<void> | void;\n keyFile: string;\n sessionDirectory: string;\n};\n\nexport type StoredSessionArchive = {\n bytes: Uint8Array;\n};\n\nexport type EncryptableSession = {\n toEncryptedArchiveAt(\n key: Uint8Array,\n observedAtEpochSeconds: number,\n ): Uint8Array;\n};\n\nexport type EncryptedSessionRestorer<Session> = {\n restoreEncryptedRedactionSession(options: {\n archive: Uint8Array;\n expectedSessionId: string;\n key: Uint8Array;\n observedAtEpochSeconds: number;\n }): Session;\n};\n\nexport type RestoreStoredSessionOptions<Session> = {\n archive: Uint8Array;\n expectedSessionId: string;\n observedAtEpochSeconds: number;\n restorer: EncryptedSessionRestorer<Session>;\n};\n\nexport class DurableSessionStore {\n #closePromise: Promise<void> | undefined;\n readonly #directory: DirectoryIdentity;\n readonly #faultInjector:\n | ((point: DurableSessionFaultPoint) => Promise<void> | void)\n | undefined;\n readonly #key: Uint8Array;\n readonly #lockHandle: FileHandle;\n #mutationTail: Promise<void> = Promise.resolve();\n #state: \"closed\" | \"closing\" | \"open\" = \"open\";\n\n private constructor(\n directory: DirectoryIdentity,\n key: Uint8Array,\n lockHandle: FileHandle,\n faultInjector?: (point: DurableSessionFaultPoint) => Promise<void> | void,\n ) {\n this.#directory = directory;\n this.#key = key;\n this.#lockHandle = lockHandle;\n this.#faultInjector = faultInjector;\n }\n\n static async create({\n keyFile,\n sessionDirectory,\n faultInjector,\n }: DurableSessionStoreOptions): Promise<DurableSessionStore> {\n assertPosixDurabilitySupport();\n const directoryPath = await canonicalAbsolutePath(\n sessionDirectory,\n \"MCP session directory\",\n );\n const metadata = await lstat(directoryPath);\n if (!metadata.isDirectory() || metadata.isSymbolicLink()) {\n throw new Error(\"MCP session directory must be a directory\");\n }\n assertOwner(metadata.uid, \"MCP session directory\");\n assertPrivateMode(metadata.mode, \"MCP session directory\");\n const key = await readKey(keyFile);\n let lockHandle: FileHandle | undefined;\n try {\n lockHandle = await acquireDirectoryLock(directoryPath);\n const store = new DurableSessionStore(\n { dev: metadata.dev, ino: metadata.ino, path: directoryPath },\n key,\n lockHandle,\n faultInjector,\n );\n await store.#validateInventory({ removeStagingFiles: true });\n await store.#syncDirectory();\n return store;\n } catch (error) {\n key.fill(0);\n await lockHandle?.close().catch(() => undefined);\n throw error;\n }\n }\n\n seal(\n session: EncryptableSession,\n observedAtEpochSeconds: number,\n ): Uint8Array {\n this.#assertOpen();\n return session.toEncryptedArchiveAt(this.#key, observedAtEpochSeconds);\n }\n\n restore<Session>({\n archive,\n expectedSessionId,\n observedAtEpochSeconds,\n restorer,\n }: RestoreStoredSessionOptions<Session>): Session {\n this.#assertOpen();\n return restorer.restoreEncryptedRedactionSession({\n archive,\n expectedSessionId,\n key: this.#key,\n observedAtEpochSeconds,\n });\n }\n\n async close(): Promise<void> {\n if (this.#closePromise !== undefined) {\n return this.#closePromise;\n }\n this.#state = \"closing\";\n this.#closePromise = (async () => {\n await this.#mutationTail;\n this.#key.fill(0);\n await this.#lockHandle.close();\n this.#state = \"closed\";\n })();\n return this.#closePromise;\n }\n\n async load(sessionId: string): Promise<StoredSessionArchive | undefined> {\n this.#assertOpen();\n return this.#withMutation(() => this.#load(sessionId));\n }\n\n async #load(sessionId: string): Promise<StoredSessionArchive | undefined> {\n assertSessionId(sessionId);\n await this.#validateInventory({ removeStagingFiles: false });\n const path = this.#path(sessionId);\n let handle: Awaited<ReturnType<typeof open>>;\n try {\n handle = await open(\n path,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw new Error(\"Encrypted session archive could not be opened\", {\n cause: error,\n });\n }\n try {\n const metadata = await handle.stat();\n if (!metadata.isFile()) {\n throw new Error(\"Encrypted session archive must be a regular file\");\n }\n assertOwner(metadata.uid, \"Encrypted session archive\");\n assertPrivateMode(metadata.mode, \"Encrypted session archive\");\n if (metadata.size > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n const bytes = await readHandleBounded(handle, SESSION_ARCHIVE_MAX_BYTES);\n await this.#assertDirectory();\n const currentMetadata = await lstat(path);\n if (!currentMetadata.isFile() || !sameFile(metadata, currentMetadata)) {\n throw new Error(\"Encrypted session archive changed while it was read\");\n }\n return { bytes };\n } finally {\n await handle.close();\n }\n }\n\n async save(sessionId: string, archive: Uint8Array): Promise<void> {\n this.#assertOpen();\n return this.#withMutation(() => this.#save(sessionId, archive));\n }\n\n async #save(sessionId: string, archive: Uint8Array): Promise<void> {\n assertSessionId(sessionId);\n if (archive.byteLength > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n const inventory = await this.#validateInventory({\n removeStagingFiles: false,\n });\n await this.#assertDirectory();\n const destination = this.#path(sessionId);\n const temporary = `${destination}.tmp.${randomUUID()}`;\n const handle = await open(\n temporary,\n fsConstants.O_WRONLY |\n fsConstants.O_CREAT |\n fsConstants.O_EXCL |\n fsConstants.O_NOFOLLOW,\n 0o600,\n );\n try {\n const openedMetadata = await handle.stat();\n if (!openedMetadata.isFile()) {\n throw new Error(\"Encrypted session staging path is not a regular file\");\n }\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeStagingWrite);\n await handle.writeFile(archive);\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeStagingFsync);\n await handle.sync();\n const stagedMetadata = await lstat(temporary);\n if (\n !stagedMetadata.isFile() ||\n !sameFile(openedMetadata, stagedMetadata)\n ) {\n throw new Error(\n \"Encrypted session staging file changed before publication\",\n );\n }\n await this.#assertDirectory();\n const existing = await lstat(destination).catch((error: unknown) => {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw error;\n });\n if (\n existing !== undefined &&\n (!existing.isFile() || existing.isSymbolicLink())\n ) {\n throw new Error(\"Encrypted session archive path is not a regular file\");\n }\n const nextCount =\n inventory.archiveCount + (existing === undefined ? 1 : 0);\n const nextTotalBytes =\n inventory.totalBytes - (existing?.size ?? 0) + archive.byteLength;\n if (nextCount > SESSION_ARCHIVE_MAX_COUNT) {\n throw new Error(\n `MCP session archives must not exceed ${SESSION_ARCHIVE_MAX_COUNT}`,\n );\n }\n if (nextTotalBytes > SESSION_ARCHIVE_TOTAL_MAX_BYTES) {\n throw new Error(\"MCP session archives exceed the aggregate byte limit\");\n }\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeRename);\n await rename(temporary, destination);\n await this.#assertDirectory();\n const published = await lstat(destination);\n if (!published.isFile() || !sameFile(openedMetadata, published)) {\n throw new Error(\"Encrypted session archive publication was not atomic\");\n }\n await this.#syncDirectory();\n } finally {\n await handle.close().catch(() => undefined);\n await unlink(temporary).catch(() => undefined);\n }\n }\n\n async delete(sessionId: string): Promise<void> {\n this.#assertOpen();\n return this.#withMutation(() => this.#delete(sessionId));\n }\n\n async #delete(sessionId: string): Promise<void> {\n assertSessionId(sessionId);\n await this.#assertDirectory();\n const path = this.#path(sessionId);\n const metadata = await lstat(path).catch((error: unknown) => {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw error;\n });\n if (metadata === undefined) {\n return;\n }\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\"Encrypted session archive path is not a regular file\");\n }\n await unlink(path);\n await this.#syncDirectory();\n await this.#assertDirectory();\n }\n\n #path(sessionId: string): string {\n return join(this.#directory.path, archiveName(sessionId));\n }\n\n #assertOpen(): void {\n if (this.#state !== \"open\") {\n throw new Error(\"MCP durable session store is closing or closed\");\n }\n }\n\n async #withMutation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n const previous = this.#mutationTail;\n let release = (): void => undefined;\n const current = new Promise<void>((resolvePromise) => {\n release = resolvePromise;\n });\n this.#mutationTail = previous.then(() => current);\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n async #assertDirectory(): Promise<void> {\n const canonical = await realpath(this.#directory.path);\n const metadata = await lstat(this.#directory.path);\n if (\n canonical !== this.#directory.path ||\n !metadata.isDirectory() ||\n metadata.isSymbolicLink() ||\n !sameFile(this.#directory, metadata)\n ) {\n throw new Error(\"MCP session directory changed while it was being used\");\n }\n assertOwner(metadata.uid, \"MCP session directory\");\n assertPrivateMode(metadata.mode, \"MCP session directory\");\n }\n\n async #syncDirectory(): Promise<void> {\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeDirectoryFsync);\n const handle = await open(\n this.#directory.path,\n fsConstants.O_RDONLY | fsConstants.O_DIRECTORY | fsConstants.O_NOFOLLOW,\n );\n try {\n const metadata = await handle.stat();\n if (!metadata.isDirectory() || !sameFile(this.#directory, metadata)) {\n throw new Error(\"MCP session directory changed before synchronization\");\n }\n await handle.sync();\n } finally {\n await handle.close();\n }\n }\n\n async #inject(point: DurableSessionFaultPoint): Promise<void> {\n await this.#faultInjector?.(point);\n }\n\n async #validateInventory({\n removeStagingFiles,\n }: {\n removeStagingFiles: boolean;\n }): Promise<SessionInventory> {\n await this.#assertDirectory();\n const entries = await readdir(this.#directory.path, {\n withFileTypes: true,\n });\n let archiveCount = 0;\n let totalBytes = 0;\n for (const entry of entries) {\n const path = join(this.#directory.path, entry.name);\n if (entry.name === LOCK_FILE_NAME) {\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\"MCP session directory contains an unsafe lock path\");\n }\n assertOwner(metadata.uid, \"MCP session lock\");\n assertPrivateMode(metadata.mode, \"MCP session lock\");\n continue;\n }\n if (STAGING_NAME.test(entry.name)) {\n if (!removeStagingFiles) {\n throw new Error(\"MCP session directory contains a partial archive\");\n }\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\n \"MCP session directory contains an unsafe staging path\",\n );\n }\n await unlink(path);\n await this.#syncDirectory();\n continue;\n }\n if (\n !ARCHIVE_NAME.test(entry.name) ||\n !entry.isFile() ||\n entry.isSymbolicLink()\n ) {\n throw new Error(\"MCP session directory contains an unsupported entry\");\n }\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\n \"MCP session directory contains an unsafe archive path\",\n );\n }\n assertOwner(metadata.uid, \"Encrypted session archive\");\n assertPrivateMode(metadata.mode, \"Encrypted session archive\");\n if (metadata.size > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n archiveCount += 1;\n totalBytes += metadata.size;\n if (archiveCount > SESSION_ARCHIVE_MAX_COUNT) {\n throw new Error(\n `MCP session archives must not exceed ${SESSION_ARCHIVE_MAX_COUNT}`,\n );\n }\n if (totalBytes > SESSION_ARCHIVE_TOTAL_MAX_BYTES) {\n throw new Error(\"MCP session archives exceed the aggregate byte limit\");\n }\n }\n await this.#assertDirectory();\n return { archiveCount, totalBytes };\n }\n}\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport {\n CAPABILITY_MANIFEST,\n type NativeCallerDetection,\n type NativeTextReplacement,\n type PreparedNativePipeline,\n} from \"@stll/anonymize\";\nimport {\n DOCX_ARCHIVE_MAX_BYTES,\n DOCX_COVERAGE_MODES,\n anonymizeDocx,\n extractDocxText,\n restoreDocxText,\n type DocxAnonymizationSession,\n type DocxRestorationSession,\n} from \"@stll/anonymize-docx\";\nimport {\n PDF_DOCUMENT_MAX_BYTES,\n anonymizePdfRaster,\n renderPdfWithPopplerTesseract,\n} from \"@stll/anonymize-pdf\";\nimport * as nativeNode from \"@stll/anonymize/native-node\";\nimport {\n constants as fsConstants,\n link,\n lstat,\n open,\n realpath,\n stat,\n unlink,\n} from \"node:fs/promises\";\nimport { randomUUID } from \"node:crypto\";\nimport {\n dirname,\n extname,\n isAbsolute,\n relative,\n resolve,\n sep,\n} from \"node:path\";\nimport * as z from \"zod/v4\";\n\nimport { DurableSessionStore } from \"./durable-sessions\";\n\nconst TEXT_MAX_BYTES = 64 * 1024 * 1024;\nconst EXTERNAL_DETECTION_BATCH_MAX_BYTES = 16 * 1024 * 1024;\nconst EXTERNAL_DETECTION_BATCH_VERSION = 1 as const;\nconst PATH_MAX_CHARACTERS = 32_768;\nconst SESSION_MAX_COUNT = 256;\nconst SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;\nconst READ_CHUNK_BYTES = 64 * 1024;\n\nconst observedAtEpochSeconds = (): number => {\n const seconds = Math.floor(Date.now() / 1000);\n if (seconds < 0 || seconds > 0xff_ff_ff_ff) {\n throw new Error(\"The current time is outside the supported session range\");\n }\n return seconds;\n};\n\nexport const MCP_SESSION_MODES = {\n durableEncrypted: \"durable-encrypted\",\n memory: \"memory\",\n} as const;\n\nexport type McpSessionMode =\n (typeof MCP_SESSION_MODES)[keyof typeof MCP_SESSION_MODES];\n\nexport type AuditSafeResult = {\n operation: \"anonymize\" | \"inspect\" | \"restore\";\n format: \"docx\" | \"pdf\" | \"text\";\n outputCreated: boolean;\n sessionId?: string;\n entityCount?: number;\n blockCount?: number;\n rewrittenBlockCount?: number;\n restoredPlaceholderCount?: number;\n coverageStatus?: \"full\" | \"partial\";\n externalDetectionBatchStatus?: \"accepted\";\n externalDetectionCount?: number;\n retainedExternalDetectionCount?: number;\n pageCount?: number;\n mappedRegionCount?: number;\n structurePixelRewriteVerified?: true;\n piiCleanGuaranteed?: false;\n};\n\nconst EXTERNAL_DETECTION_FAILURES = {\n batchRejected: {\n code: \"EXTERNAL_DETECTION_BATCH_REJECTED\",\n message: \"The external detection batch was rejected.\",\n },\n documentRejected: {\n code: \"EXTERNAL_DETECTION_DOCUMENT_REJECTED\",\n message: \"The external detection document was rejected.\",\n },\n inputRejected: {\n code: \"EXTERNAL_DETECTION_INPUT_REJECTED\",\n message: \"The external detection request paths were rejected.\",\n },\n operationFailed: {\n code: \"EXTERNAL_DETECTION_OPERATION_FAILED\",\n message: \"The external detection operation failed safely.\",\n },\n sessionRejected: {\n code: \"EXTERNAL_DETECTION_SESSION_REJECTED\",\n message: \"The external detection session was rejected.\",\n },\n} as const;\n\ntype ExternalDetectionFailure =\n (typeof EXTERNAL_DETECTION_FAILURES)[keyof typeof EXTERNAL_DETECTION_FAILURES];\n\nclass ExternalDetectionAuditError extends Error {\n readonly code: ExternalDetectionFailure[\"code\"];\n\n constructor(failure: ExternalDetectionFailure) {\n super(failure.message);\n this.name = \"ExternalDetectionAuditError\";\n this.code = failure.code;\n }\n}\n\nconst externalDetectionFailure = (\n error: unknown,\n failure: ExternalDetectionFailure,\n): ExternalDetectionAuditError =>\n error instanceof ExternalDetectionAuditError\n ? error\n : new ExternalDetectionAuditError(failure);\n\nconst externalDetectionStep = async <Result>(\n failure: ExternalDetectionFailure,\n operation: () => Result | Promise<Result>,\n): Promise<Result> => {\n try {\n return await operation();\n } catch (error) {\n throw externalDetectionFailure(error, failure);\n }\n};\n\nexport type LocalAnonymizeServiceFaults = {\n beforeOutputPublish?: () => void | Promise<void>;\n};\n\nconst inside = (root: string, target: string): boolean => {\n const path = relative(root, target);\n return (\n path === \"\" ||\n (path !== \"..\" && !path.startsWith(`..${sep}`) && !isAbsolute(path))\n );\n};\n\ntype ReadInputOptions = {\n path: string;\n extension: \".docx\" | \".json\" | \".pdf\" | \".txt\";\n maximumBytes: number;\n label: \"DOCX\" | \"External detection batch\" | \"PDF\" | \"Text\";\n};\n\ntype ScopedInput = {\n bytes: Uint8Array;\n path: string;\n};\n\ntype ReadableFileHandle = Pick<Awaited<ReturnType<typeof open>>, \"read\">;\n\nconst readHandleBounded = async (\n handle: ReadableFileHandle,\n maximumBytes: number,\n label: \"DOCX\" | \"External detection batch\" | \"PDF\" | \"Text\",\n): Promise<Uint8Array> => {\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const chunk = Buffer.allocUnsafe(\n Math.min(READ_CHUNK_BYTES, maximumBytes - total + 1),\n );\n const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null);\n if (bytesRead === 0) {\n return Buffer.concat(chunks, total);\n }\n total += bytesRead;\n if (total > maximumBytes) {\n throw new Error(`${label} inputs must not exceed ${maximumBytes} bytes`);\n }\n chunks.push(chunk.subarray(0, bytesRead));\n }\n};\n\ntype DirectoryIdentity = {\n dev: number;\n ino: number;\n path: string;\n};\n\ntype FileIdentity = Pick<DirectoryIdentity, \"dev\" | \"ino\">;\n\nclass ScopedOutput {\n readonly parent: DirectoryIdentity;\n readonly path: string;\n\n constructor(path: string, parent: DirectoryIdentity) {\n this.path = path;\n this.parent = parent;\n }\n\n async write(bytes: Uint8Array | string): Promise<void> {\n await safeWrite(this, bytes);\n }\n}\n\nexport class PathScope {\n readonly #roots: readonly string[];\n\n private constructor(roots: readonly string[]) {\n this.#roots = roots;\n }\n\n static async create(roots: readonly string[]): Promise<PathScope> {\n if (roots.length === 0) {\n throw new Error(\"At least one --root directory is required\");\n }\n const canonical = await Promise.all(\n roots.map(async (root) => {\n if (!isAbsolute(root)) {\n throw new Error(\"MCP roots must be absolute paths\");\n }\n const path = await realpath(root);\n const metadata = await stat(path);\n if (!metadata.isDirectory()) {\n throw new Error(\"Every MCP root must be a directory\");\n }\n return path;\n }),\n );\n return new PathScope([...new Set(canonical)]);\n }\n\n async readInput({\n path,\n extension,\n maximumBytes,\n label,\n }: ReadInputOptions): Promise<ScopedInput> {\n if (!isAbsolute(path) || extname(path).toLowerCase() !== extension) {\n throw new Error(`Input must be an absolute ${extension} path`);\n }\n const initiallyCanonical = await realpath(path);\n if (!this.#roots.some((root) => inside(root, initiallyCanonical))) {\n throw new Error(\"Input is outside the configured roots\");\n }\n const initiallyRequestedMetadata = await lstat(path);\n if (!initiallyRequestedMetadata.isFile()) {\n throw new Error(\"Input must be a regular file\");\n }\n const handle = await open(\n path,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n try {\n const openedMetadata = await handle.stat();\n if (!openedMetadata.isFile()) {\n throw new Error(\"Input must be a regular file\");\n }\n const canonical = await realpath(path);\n if (!this.#roots.some((root) => inside(root, canonical))) {\n throw new Error(\"Input is outside the configured roots\");\n }\n const currentMetadata = await stat(canonical);\n if (\n currentMetadata.dev !== openedMetadata.dev ||\n currentMetadata.ino !== openedMetadata.ino\n ) {\n throw new Error(\"Input changed while it was being validated\");\n }\n if (openedMetadata.size > maximumBytes) {\n throw new Error(\n `${label} inputs must not exceed ${maximumBytes} bytes`,\n );\n }\n const bytes = await readHandleBounded(handle, maximumBytes, label);\n return { bytes, path: canonical };\n } finally {\n await handle.close();\n }\n }\n\n async output(\n path: string,\n extension: \".docx\" | \".pdf\" | \".txt\",\n ): Promise<ScopedOutput> {\n if (!isAbsolute(path) || extname(path).toLowerCase() !== extension) {\n throw new Error(`Output must be an absolute ${extension} path`);\n }\n const normalized = resolve(path);\n const parent = await realpath(dirname(normalized));\n if (!this.#roots.some((root) => inside(root, parent))) {\n throw new Error(\"Output is outside the configured roots\");\n }\n const parentMetadata = await stat(parent);\n try {\n await lstat(normalized);\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return new ScopedOutput(normalized, {\n dev: parentMetadata.dev,\n ino: parentMetadata.ino,\n path: parent,\n });\n }\n throw new Error(\"Output availability could not be verified\", {\n cause: error,\n });\n }\n throw new Error(\"Output already exists; overwriting is not supported\");\n }\n}\n\ntype SessionEntry = {\n language: string | undefined;\n session: RedactionSession;\n restoreSession: (plaintextJson: string) => RedactionSession;\n status: \"busy\" | \"initializing\" | \"ready\";\n};\n\ntype SessionLease = {\n entry: SessionEntry;\n sessionId: string;\n rollback:\n | { type: \"delete\" }\n | { type: \"release\" }\n | { type: \"restore\"; checkpoint: string };\n};\n\ntype RedactionSession = DocxAnonymizationSession &\n DocxRestorationSession & {\n redact_text(text: string): {\n redaction: { redactedText: string; entityCount: number };\n };\n restoreText(text: string): string;\n toPlaintextJson(): string;\n toEncryptedArchiveAt(\n key: Uint8Array,\n observedAtEpochSeconds: number,\n ): Uint8Array;\n };\n\ntype NativeNodeSurface = {\n convert_external_detection_batch: (\n document: Uint8Array,\n batch: string,\n ) => NativeCallerDetection[];\n getDefaultNativePipeline: (options: { language?: string }) => {\n createRedactionSession: (sessionId: string) => RedactionSession;\n restoreRedactionSession: (plaintextJson: string) => RedactionSession;\n restoreEncryptedRedactionSession: (options: {\n archive: Uint8Array;\n expectedSessionId: string;\n key: Uint8Array;\n observedAtEpochSeconds: number;\n }) => RedactionSession;\n } & Pick<\n PreparedNativePipeline,\n \"redactText\" | \"redactTextWithCallerDetections\"\n >;\n native_package_version: () => string;\n};\n\nconst nativeNodeSurface = nativeNode as unknown as NativeNodeSurface; // SAFETY: These native-node runtime exports are public, but their generated declarations are minified during workspace builds.\n\nconst assertOutputParent = async ({\n parent,\n path,\n}: ScopedOutput): Promise<void> => {\n const canonical = await realpath(dirname(path));\n const metadata = await stat(canonical);\n if (\n canonical !== parent.path ||\n metadata.dev !== parent.dev ||\n metadata.ino !== parent.ino\n ) {\n throw new Error(\"Output directory changed while it was being used\");\n }\n};\n\nconst sameFile = (left: FileIdentity, right: FileIdentity): boolean =>\n left.dev === right.dev && left.ino === right.ino;\n\nconst safeWrite = async (\n output: ScopedOutput,\n bytes: Uint8Array | string,\n): Promise<void> => {\n await assertOutputParent(output);\n const temporary = `${output.path}.stella-${randomUUID()}.tmp`;\n let published = false;\n let committed = false;\n let temporaryMetadata: FileIdentity | undefined;\n const handle = await open(\n temporary,\n fsConstants.O_WRONLY |\n fsConstants.O_CREAT |\n fsConstants.O_EXCL |\n fsConstants.O_NOFOLLOW,\n 0o400,\n );\n try {\n temporaryMetadata = await handle.stat();\n const canonicalTemporary = await realpath(temporary);\n const currentTemporaryMetadata = await lstat(temporary);\n if (\n dirname(canonicalTemporary) !== output.parent.path ||\n !currentTemporaryMetadata.isFile() ||\n !sameFile(temporaryMetadata, currentTemporaryMetadata)\n ) {\n throw new Error(\"Output staging file changed while it was being used\");\n }\n await handle.writeFile(bytes);\n await handle.sync();\n await assertOutputParent(output);\n const stagedMetadata = await lstat(temporary);\n if (\n !stagedMetadata.isFile() ||\n !sameFile(temporaryMetadata, stagedMetadata)\n ) {\n throw new Error(\"Output staging file changed before publication\");\n }\n await link(temporary, output.path);\n published = true;\n const publishedMetadata = await lstat(output.path);\n if (\n !publishedMetadata.isFile() ||\n !sameFile(temporaryMetadata, publishedMetadata)\n ) {\n throw new Error(\"Published output does not match the staged file\");\n }\n await assertOutputParent(output);\n await handle.chmod(0o600);\n await handle.sync();\n committed = true;\n } finally {\n if (!committed) {\n await handle.truncate(0).catch(() => undefined);\n await handle.sync().catch(() => undefined);\n await handle.chmod(0o000).catch(() => undefined);\n }\n await handle.close().catch(() => undefined);\n await unlink(temporary).catch(() => undefined);\n if (published && !committed && temporaryMetadata !== undefined) {\n const publishedMetadata = await lstat(output.path).catch(() => undefined);\n if (\n publishedMetadata !== undefined &&\n sameFile(temporaryMetadata, publishedMetadata)\n ) {\n await unlink(output.path).catch(() => undefined);\n }\n }\n }\n};\n\nconst decodeText = (bytes: Uint8Array): string => {\n return decodeUtf8(bytes, \"Text inputs\");\n};\n\nconst decodeUtf8 = (bytes: Uint8Array, label: string): string => {\n try {\n return new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n } catch (error) {\n throw new Error(`${label} must contain valid UTF-8`, { cause: error });\n }\n};\n\nconst applyTextReplacements = (\n text: string,\n replacements: readonly NativeTextReplacement[],\n): string => {\n const parts: string[] = [];\n let cursor = 0;\n for (const replacement of replacements) {\n if (\n !Number.isSafeInteger(replacement.start) ||\n !Number.isSafeInteger(replacement.end) ||\n replacement.start < cursor ||\n replacement.end <= replacement.start ||\n replacement.end > text.length ||\n !isUtf16Boundary(text, replacement.start) ||\n !isUtf16Boundary(text, replacement.end)\n ) {\n throw new Error(\n \"Native caller-detection plan returned invalid replacements\",\n );\n }\n parts.push(text.slice(cursor, replacement.start), replacement.replacement);\n cursor = replacement.end;\n }\n parts.push(text.slice(cursor));\n return parts.join(\"\");\n};\n\nconst isUtf16Boundary = (text: string, offset: number): boolean => {\n if (offset <= 0 || offset >= text.length) {\n return true;\n }\n const before = text.charCodeAt(offset - 1);\n const after = text.charCodeAt(offset);\n return !(\n before >= 0xd800 &&\n before <= 0xdbff &&\n after >= 0xdc00 &&\n after <= 0xdfff\n );\n};\n\nconst assertDifferentPaths = (input: string, output: string): void => {\n if (input === output) {\n throw new Error(\"Input and output paths must differ\");\n }\n};\n\nconst textInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n sessionId: z.string().regex(SESSION_ID),\n language: z.string().min(2).max(35).optional(),\n});\n\nconst externalDetectionTextInput = textInput.extend({\n detectionBatchPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n});\n\nconst restoreInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n sessionId: z.string().regex(SESSION_ID),\n});\n\nconst docxRestoreInput = restoreInput.extend({\n allowPartialCoverage: z.boolean().optional().default(false),\n});\n\nconst docxInput = textInput.extend({\n allowPartialCoverage: z.boolean().optional().default(false),\n});\n\nconst pdfInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n ocrLanguage: z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_.-]*$/u),\n detectionLanguage: z.string().min(2).max(35).optional(),\n dpi: z.number().int().min(72).max(600).optional().default(300),\n timeoutMs: z.number().int().min(100).max(300_000).optional().default(120_000),\n fillRgb: z\n .tuple([\n z.number().int().min(0).max(255),\n z.number().int().min(0).max(255),\n z.number().int().min(0).max(255),\n ])\n .optional()\n .default([0, 0, 0]),\n});\n\nexport type LocalPdfProviderConfiguration = {\n pdftoppmPath?: string | undefined;\n tesseractPath?: string | undefined;\n};\n\nexport type LocalAnonymizeServiceOptions = {\n durableSessions?: DurableSessionStore | undefined;\n faults?: LocalAnonymizeServiceFaults | undefined;\n pdfProvider?: LocalPdfProviderConfiguration | undefined;\n};\n\nexport class LocalAnonymizeService {\n #activeOperations = 0;\n #closePromise: Promise<void> | undefined;\n #operationsDrained: (() => void) | undefined;\n readonly #scope: PathScope;\n #pdfOperationTail: Promise<void> = Promise.resolve();\n readonly #sessionInitializations = new Set<string>();\n readonly #sessions = new Map<string, SessionEntry>();\n #state: \"closed\" | \"closing\" | \"open\" = \"open\";\n readonly #durableSessions: DurableSessionStore | undefined;\n readonly #faults: LocalAnonymizeServiceFaults;\n readonly #pdfProvider: LocalPdfProviderConfiguration;\n\n constructor(scope: PathScope, options: LocalAnonymizeServiceOptions = {}) {\n if (options instanceof DurableSessionStore) {\n throw new TypeError(\n \"LocalAnonymizeService requires { durableSessions } as its second argument\",\n );\n }\n const { durableSessions, faults = {}, pdfProvider = {} } = options;\n this.#scope = scope;\n this.#durableSessions = durableSessions;\n this.#faults = faults;\n this.#pdfProvider = pdfProvider;\n }\n\n get sessionMode(): McpSessionMode {\n return this.#durableSessions === undefined\n ? MCP_SESSION_MODES.memory\n : MCP_SESSION_MODES.durableEncrypted;\n }\n\n async close(): Promise<void> {\n if (this.#closePromise !== undefined) {\n return this.#closePromise;\n }\n this.#state = \"closing\";\n this.#closePromise = (async () => {\n if (this.#activeOperations > 0) {\n await new Promise<void>((resolvePromise) => {\n this.#operationsDrained = resolvePromise;\n });\n }\n this.#sessions.clear();\n await this.#durableSessions?.close();\n this.#state = \"closed\";\n })();\n return this.#closePromise;\n }\n\n async #runOperation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#state !== \"open\") {\n throw new Error(\"MCP anonymize service is closing or closed\");\n }\n this.#activeOperations += 1;\n try {\n return await operation();\n } finally {\n this.#activeOperations -= 1;\n if (this.#activeOperations === 0) {\n this.#operationsDrained?.();\n this.#operationsDrained = undefined;\n }\n }\n }\n\n async #session(sessionId: string, language?: string): Promise<SessionLease> {\n if (this.#durableSessions !== undefined && language !== undefined) {\n throw new Error(\n \"Durable sessions use the full all-language pipeline; omit language\",\n );\n }\n const existing = this.#sessions.get(sessionId);\n if (existing !== undefined) {\n if (language !== undefined && existing.language !== language) {\n throw new Error(\"A session cannot change language\");\n }\n if (existing.status === \"initializing\") {\n throw new Error(\"The requested session is still initializing\");\n }\n if (existing.status === \"busy\") {\n throw new Error(\"The requested session is handling another operation\");\n }\n const checkpoint = existing.session.toPlaintextJson();\n existing.status = \"busy\";\n return {\n entry: existing,\n sessionId,\n rollback: { type: \"restore\", checkpoint },\n };\n }\n if (this.#sessionInitializations.has(sessionId)) {\n throw new Error(\"The requested session is still initializing\");\n }\n this.#sessionInitializations.add(sessionId);\n try {\n if (this.#sessions.size >= SESSION_MAX_COUNT) {\n throw new Error(`MCP sessions must not exceed ${SESSION_MAX_COUNT}`);\n }\n const pipeline = nativeNodeSurface.getDefaultNativePipeline(\n language === undefined ? {} : { language },\n );\n const durableSessions = this.#durableSessions;\n const stored =\n durableSessions === undefined\n ? undefined\n : await durableSessions.load(sessionId);\n let session: RedactionSession;\n if (stored === undefined) {\n session = pipeline.createRedactionSession(sessionId);\n } else {\n if (durableSessions === undefined) {\n throw new Error(\"Durable session storage is unavailable\");\n }\n try {\n session = durableSessions.restore({\n archive: stored.bytes,\n expectedSessionId: sessionId,\n observedAtEpochSeconds: observedAtEpochSeconds(),\n restorer: pipeline,\n });\n } catch (error) {\n throw new Error(\"The requested durable session is unavailable\", {\n cause: error,\n });\n }\n }\n const entry: SessionEntry = {\n language,\n session,\n restoreSession: (plaintextJson) =>\n pipeline.restoreRedactionSession(plaintextJson),\n status: \"initializing\",\n };\n this.#sessions.set(sessionId, entry);\n return {\n entry,\n sessionId,\n rollback:\n stored === undefined\n ? { type: \"delete\" }\n : { type: \"restore\", checkpoint: session.toPlaintextJson() },\n };\n } finally {\n this.#sessionInitializations.delete(sessionId);\n }\n }\n\n #commitSession({ entry }: SessionLease): void {\n entry.status = \"ready\";\n }\n\n async #rollbackSession({\n entry,\n rollback,\n sessionId,\n }: SessionLease): Promise<void> {\n if (this.#sessions.get(sessionId) !== entry) {\n return;\n }\n if (rollback.type === \"delete\") {\n this.#sessions.delete(sessionId);\n await this.#durableSessions?.delete(sessionId).catch(() => undefined);\n return;\n }\n if (rollback.type === \"release\") {\n entry.status = \"ready\";\n return;\n }\n try {\n entry.session = entry.restoreSession(rollback.checkpoint);\n entry.status = \"ready\";\n await this.#persistSession(sessionId, entry.session);\n } catch {\n this.#sessions.delete(sessionId);\n }\n }\n\n async #readSession(sessionId: string): Promise<SessionLease> {\n let entry = this.#sessions.get(sessionId);\n if (entry === undefined && this.#durableSessions !== undefined) {\n if (this.#sessionInitializations.has(sessionId)) {\n throw new Error(\"The requested session is still initializing\");\n }\n this.#sessionInitializations.add(sessionId);\n try {\n const pipeline = nativeNodeSurface.getDefaultNativePipeline({});\n const stored = await this.#durableSessions.load(sessionId);\n if (stored !== undefined) {\n try {\n const session = this.#durableSessions.restore({\n archive: stored.bytes,\n expectedSessionId: sessionId,\n observedAtEpochSeconds: observedAtEpochSeconds(),\n restorer: pipeline,\n });\n entry = {\n language: undefined,\n session,\n restoreSession: (plaintextJson) =>\n pipeline.restoreRedactionSession(plaintextJson),\n status: \"ready\",\n };\n this.#sessions.set(sessionId, entry);\n } catch (error) {\n throw new Error(\"The requested durable session is unavailable\", {\n cause: error,\n });\n }\n }\n } finally {\n this.#sessionInitializations.delete(sessionId);\n }\n }\n if (entry === undefined) {\n throw new Error(\"The requested session is unavailable\");\n }\n if (entry.status !== \"ready\") {\n throw new Error(\"The requested in-memory session is unavailable\");\n }\n entry.status = \"busy\";\n return { entry, sessionId, rollback: { type: \"release\" } };\n }\n\n async #persistSession(\n sessionId: string,\n session: RedactionSession,\n ): Promise<void> {\n if (this.#durableSessions === undefined) {\n return;\n }\n const archive = this.#durableSessions.seal(\n session,\n observedAtEpochSeconds(),\n );\n await this.#durableSessions.save(sessionId, archive);\n }\n\n async anonymizeText(\n input: z.infer<typeof textInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#anonymizeText(input));\n }\n\n async anonymizePdf(\n input: z.infer<typeof pdfInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() =>\n this.#serializePdfOperation(() => this.#anonymizePdf(input)),\n );\n }\n\n async #serializePdfOperation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n const previous = this.#pdfOperationTail;\n let release = (): void => undefined;\n this.#pdfOperationTail = new Promise<void>((resolvePromise) => {\n release = resolvePromise;\n });\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n async #anonymizePdf(\n input: z.infer<typeof pdfInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".pdf\",\n maximumBytes: PDF_DOCUMENT_MAX_BYTES,\n label: \"PDF\",\n });\n const destination = await this.#scope.output(input.outputPath, \".pdf\");\n assertDifferentPaths(source.path, destination.path);\n const pipeline = nativeNodeSurface.getDefaultNativePipeline(\n input.detectionLanguage === undefined\n ? {}\n : { language: input.detectionLanguage },\n );\n const observed = await renderPdfWithPopplerTesseract({\n document: source.bytes,\n ocrLanguage: input.ocrLanguage,\n dpi: input.dpi,\n timeoutMs: input.timeoutMs,\n ...this.#pdfProvider,\n });\n const anonymized = anonymizePdfRaster({\n document: source.bytes,\n pipeline,\n provider: observed.provider,\n pages: observed.pages,\n fillRgb: input.fillRgb,\n });\n if (\n anonymized.certificate.structurePixelRewriteVerified !== true ||\n anonymized.certificate.piiCleanGuaranteed !== false\n ) {\n throw new Error(\"PDF raster verification did not satisfy MCP policy\");\n }\n await this.#faults.beforeOutputPublish?.();\n await destination.write(anonymized.document);\n return {\n operation: \"anonymize\",\n format: \"pdf\",\n outputCreated: true,\n pageCount: anonymized.certificate.pageCount,\n entityCount: anonymized.certificate.detectionCount,\n mappedRegionCount: anonymized.certificate.mappedRegionCount,\n structurePixelRewriteVerified: true,\n piiCleanGuaranteed: false,\n };\n }\n\n async #anonymizeText(\n input: z.infer<typeof textInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const destination = await this.#scope.output(input.outputPath, \".txt\");\n assertDifferentPaths(source.path, destination.path);\n const text = decodeText(source.bytes);\n const lease = await this.#session(input.sessionId, input.language);\n try {\n const result = lease.entry.session.redact_text(text);\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(result.redaction.redactedText);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: result.redaction.entityCount,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async restoreText(\n input: z.infer<typeof restoreInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#restoreText(input));\n }\n\n async anonymizeTextWithExternalDetections(\n input: z.infer<typeof externalDetectionTextInput>,\n ): Promise<AuditSafeResult> {\n try {\n return await this.#runOperation(() =>\n this.#anonymizeTextWithExternalDetections(input),\n );\n } catch (error) {\n throw externalDetectionFailure(\n error,\n EXTERNAL_DETECTION_FAILURES.operationFailed,\n );\n }\n }\n\n async #anonymizeTextWithExternalDetections(\n input: z.infer<typeof externalDetectionTextInput>,\n ): Promise<AuditSafeResult> {\n const { batch, destination, source } = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.inputRejected,\n async () => {\n const scopedSource = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const scopedBatch = await this.#scope.readInput({\n path: input.detectionBatchPath,\n extension: \".json\",\n maximumBytes: EXTERNAL_DETECTION_BATCH_MAX_BYTES,\n label: \"External detection batch\",\n });\n const scopedDestination = await this.#scope.output(\n input.outputPath,\n \".txt\",\n );\n assertDifferentPaths(scopedSource.path, scopedDestination.path);\n assertDifferentPaths(scopedBatch.path, scopedDestination.path);\n assertDifferentPaths(scopedSource.path, scopedBatch.path);\n return {\n batch: scopedBatch,\n destination: scopedDestination,\n source: scopedSource,\n };\n },\n );\n const text = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.documentRejected,\n () => decodeText(source.bytes),\n );\n const detections = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.batchRejected,\n () =>\n nativeNodeSurface.convert_external_detection_batch(\n source.bytes,\n decodeUtf8(batch.bytes, \"External detection batches\"),\n ),\n );\n const lease = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.sessionRejected,\n () => this.#session(input.sessionId, input.language),\n );\n try {\n const plan = lease.entry.session.planTextBatchWithCallerDetections({\n inputs: [{ fullText: text, detections }],\n });\n const block = plan.blocks.at(0);\n if (plan.blocks.length !== 1 || block === undefined) {\n throw new Error(\n \"Native caller-detection plan did not match the text input\",\n );\n }\n const redactedText = applyTextReplacements(text, block.replacements);\n plan.commit();\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(redactedText);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: block.entityCount,\n externalDetectionBatchStatus: \"accepted\",\n externalDetectionCount: detections.length,\n retainedExternalDetectionCount: block.callerEntityCount,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw externalDetectionFailure(\n error,\n EXTERNAL_DETECTION_FAILURES.operationFailed,\n );\n }\n }\n\n async #restoreText(\n input: z.infer<typeof restoreInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const destination = await this.#scope.output(input.outputPath, \".txt\");\n assertDifferentPaths(source.path, destination.path);\n const text = decodeText(source.bytes);\n const lease = await this.#readSession(input.sessionId);\n try {\n const restored = lease.entry.session.restoreText(text);\n await destination.write(restored);\n this.#commitSession(lease);\n return {\n operation: \"restore\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async anonymizeDocx(\n input: z.infer<typeof docxInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#anonymizeDocx(input));\n }\n\n async #anonymizeDocx(\n input: z.infer<typeof docxInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const destination = await this.#scope.output(input.outputPath, \".docx\");\n assertDifferentPaths(source.path, destination.path);\n const lease = await this.#session(input.sessionId, input.language);\n try {\n const result = anonymizeDocx({\n document: source.bytes,\n session: lease.entry.session,\n expectedSessionId: input.sessionId,\n policy: {\n coverage: {\n mode: input.allowPartialCoverage\n ? DOCX_COVERAGE_MODES.allowPartial\n : DOCX_COVERAGE_MODES.requireFull,\n },\n },\n });\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(result.document);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"docx\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: result.summary.entityCount,\n blockCount: result.summary.blockCount,\n rewrittenBlockCount: result.summary.rewrittenBlockCount,\n coverageStatus: result.summary.coverage.status,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async restoreDocx(\n input: z.infer<typeof docxRestoreInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#restoreDocx(input));\n }\n\n async #restoreDocx(\n input: z.infer<typeof docxRestoreInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const destination = await this.#scope.output(input.outputPath, \".docx\");\n assertDifferentPaths(source.path, destination.path);\n const lease = await this.#readSession(input.sessionId);\n try {\n const result = restoreDocxText({\n document: source.bytes,\n session: lease.entry.session,\n expectedSessionId: input.sessionId,\n });\n if (result.coverage.status === \"partial\" && !input.allowPartialCoverage) {\n throw new Error(\n \"DOCX restoration has partial coverage; set allowPartialCoverage to publish it\",\n );\n }\n await destination.write(result.document);\n this.#commitSession(lease);\n return {\n operation: \"restore\",\n format: \"docx\",\n outputCreated: true,\n sessionId: input.sessionId,\n rewrittenBlockCount: result.restoredBlockCount,\n restoredPlaceholderCount: result.restoredPlaceholderCount,\n coverageStatus: result.coverage.status,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async inspectDocx(inputPath: string): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#inspectDocx(inputPath));\n }\n\n async #inspectDocx(inputPath: string): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const extraction = extractDocxText(source.bytes);\n const unsupported = extraction.coverage.parts.some(\n (part) => part.status === \"unsupported\",\n );\n const structuralGap =\n extraction.coverage.hyperlinkTextSegmentCount > 0 ||\n extraction.coverage.revisionTextSegmentCount > 0 ||\n extraction.coverage.unsupportedAlternateContentCount > 0 ||\n extraction.coverage.unsupportedFieldInstructionCount > 0 ||\n extraction.coverage.unsupportedSymbolCount > 0;\n return {\n operation: \"inspect\",\n format: \"docx\",\n outputCreated: false,\n blockCount: extraction.blocks.length,\n coverageStatus: unsupported || structuralGap ? \"partial\" : \"full\",\n };\n }\n}\n\nconst result = (value: AuditSafeResult) => ({\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: { ...value },\n});\n\nconst MCP_TOOL_NAMES = [\n \"anonymize_docx_file\",\n \"anonymize_pdf_file\",\n \"anonymize_text_file\",\n \"anonymize_text_file_with_external_detections\",\n \"capabilities\",\n \"inspect_docx_file\",\n \"restore_docx_file\",\n \"restore_text_file\",\n] as const;\n\nconst capabilitiesResult = (service: LocalAnonymizeService) => {\n const value = {\n capabilityManifest: CAPABILITY_MANIFEST,\n runtimeVersion: nativeNodeSurface.native_package_version(),\n mcp: {\n externalDetectionBatch: {\n ingestion: \"path-only\" as const,\n version: EXTERNAL_DETECTION_BATCH_VERSION,\n },\n formats: [\"docx\", \"pdf\", \"text\"] as const,\n sessionMode: service.sessionMode,\n tools: MCP_TOOL_NAMES,\n transport: \"stdio\" as const,\n },\n };\n return {\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: value,\n };\n};\n\nconst externalDetectionErrorResult = (error: unknown) => {\n const failure = externalDetectionFailure(\n error,\n EXTERNAL_DETECTION_FAILURES.operationFailed,\n );\n const value = { errorCode: failure.code, message: failure.message };\n return {\n isError: true,\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: value,\n };\n};\n\nexport const createAnonymizeMcpServer = (\n service: LocalAnonymizeService,\n): McpServer => {\n const server = new McpServer(\n {\n name: \"stella-anonymize-local\",\n version: nativeNodeSurface.native_package_version(),\n },\n {\n instructions:\n \"All tools accept local paths only. Never request or return document contents or session mappings. Outputs must be new explicit paths inside configured roots.\",\n },\n );\n server.registerTool(\n \"capabilities\",\n {\n description:\n \"Return the public runtime capability manifest and MCP surface metadata.\",\n inputSchema: z.object({}),\n annotations: {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n },\n },\n async () => capabilitiesResult(service),\n );\n server.registerTool(\n \"anonymize_text_file\",\n {\n description: \"Anonymize a local UTF-8 text file into a new local file.\",\n inputSchema: textInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => result(await service.anonymizeText(input)),\n );\n server.registerTool(\n \"restore_text_file\",\n {\n description: \"Restore a text file using the configured session store.\",\n inputSchema: restoreInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => result(await service.restoreText(input)),\n );\n server.registerTool(\n \"anonymize_text_file_with_external_detections\",\n {\n description:\n \"Anonymize a local UTF-8 text file with a provider-neutral ExternalDetectionBatch v1 JSON sidecar into a new local file.\",\n inputSchema: externalDetectionTextInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => {\n try {\n return result(await service.anonymizeTextWithExternalDetections(input));\n } catch (error) {\n return externalDetectionErrorResult(error);\n }\n },\n );\n server.registerTool(\n \"anonymize_docx_file\",\n {\n description:\n \"Structure-preservingly anonymize a local DOCX into a new local DOCX.\",\n inputSchema: docxInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => result(await service.anonymizeDocx(input)),\n );\n server.registerTool(\n \"anonymize_pdf_file\",\n {\n description:\n \"Destructively raster-anonymize a local PDF into a fresh image-only PDF. Returns aggregate verification only; it does not claim perfect OCR or detector recall.\",\n inputSchema: pdfInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => result(await service.anonymizePdf(input)),\n );\n server.registerTool(\n \"restore_docx_file\",\n {\n description: \"Restore a DOCX using the configured session store.\",\n inputSchema: docxRestoreInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) => result(await service.restoreDocx(input)),\n );\n server.registerTool(\n \"inspect_docx_file\",\n {\n description:\n \"Return only aggregate DOCX coverage and block counts; never document text.\",\n inputSchema: z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n }),\n annotations: {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n },\n },\n async ({ inputPath }) => result(await service.inspectDocx(inputPath)),\n );\n return server;\n};\n"],"mappings":";;;;;;;;;;AAcA,MAAa,4BAA4B;AACzC,MAAa,4BAA4B;AACzC,MAAa,4BAA4B;AACzC,MAAa,kCAAkC,MAAM,OAAO;AAE5D,MAAM,eAAe;AACrB,MAAMA,eAAa;AACnB,MAAM,eACJ;AACF,MAAMC,qBAAmB,KAAK;AAC9B,MAAM,iBAAiB;AAEvB,MAAa,+BAA+B;CAC1C,sBAAsB;CACtB,cAAc;CACd,oBAAoB;CACpB,oBAAoB;AACtB;AAmBA,MAAMC,cAAY,MAAoB,UACpC,KAAK,QAAQ,MAAM,OAAO,KAAK,QAAQ,MAAM;AAE/C,MAAM,eAAe,KAAa,UAAwB;CACxD,IAAI,OAAO,QAAQ,WAAW,cAAc,QAAQ,QAAQ,OAAO,GACjE,MAAM,IAAI,MAAM,GAAG,MAAM,mCAAmC;AAEhE;AAEA,MAAM,qBAAqB,MAAc,UAAwB;CAC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AC3D;CACA,OAAO,aACL,qBACA;EACE,aAAa;EACb,aAAa;EACb,aAAa;GAAE,iBAAiB;GAAO,gBAAgB;EAAM;CAC/D,GACA,OAAO,UAAU,OAAO,MAAM,QAAQ,YAAY,KAAK,CAAC,CAC1D;CACA,OAAO,aACL,qBACA;EACE,aACE;EACF,aAAa,EAAE,OAAO,EACpB,WAAW,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,mBAAmB,EACtD,CAAC;EACD,aAAa;GACX,cAAc;GACd,iBAAiB;GACjB,gBAAgB;EAClB;CACF,GACA,OAAO,EAAE,gBAAgB,OAAO,MAAM,QAAQ,YAAY,SAAS,CAAC,CACtE;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"local.mjs","names":["SESSION_ID","READ_CHUNK_BYTES","sameFile","fsConstants","readHandleBounded","#directory","#faultInjector","#key","#lockHandle","#validateInventory","#syncDirectory","#assertOpen","#closePromise","#state","#mutationTail","#withMutation","#load","#path","#assertDirectory","#save","#inject","#delete","#roots","fsConstants","#scope","#sessionInitializations","#sessions","#durableSessions","#faults","#pdfProvider","#closePromise","#state","#activeOperations","#operationsDrained","#runOperation","#session","#rollbackSession","#persistSession","#readSession","#anonymizeText","#serializePdfOperation","#anonymizePdf","#pdfOperationTail","#commitSession","#restoreText","#anonymizeTextWithExternalDetections","#anonymizeDocx","#restoreDocx","#inspectDocx","pkg.version"],"sources":["../package.json","../src/durable-sessions.ts","../src/local.ts"],"sourcesContent":["","import { createHash, randomUUID } from \"node:crypto\";\nimport {\n constants as fsConstants,\n lstat,\n open,\n readdir,\n realpath,\n rename,\n stat,\n unlink,\n} from \"node:fs/promises\";\nimport type { FileHandle } from \"node:fs/promises\";\nimport { isAbsolute, join, resolve } from \"node:path\";\n\nexport const SESSION_ARCHIVE_KEY_BYTES = 32;\nexport const SESSION_ARCHIVE_MAX_BYTES = 16 * 1024 * 1024 + 57;\nexport const SESSION_ARCHIVE_MAX_COUNT = 256;\nexport const SESSION_ARCHIVE_TOTAL_MAX_BYTES = 256 * 1024 * 1024;\n\nconst ARCHIVE_NAME = /^[a-f0-9]{64}\\.stlasess$/u;\nconst SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;\nconst STAGING_NAME =\n /^[a-f0-9]{64}\\.stlasess\\.tmp\\.[0-9a-f]{8}-[0-9a-f-]{27}$/u;\nconst READ_CHUNK_BYTES = 64 * 1024;\nconst LOCK_FILE_NAME = \".stella-session.lock\";\n\nexport const DURABLE_SESSION_FAULT_POINTS = {\n beforeDirectoryFsync: \"before-directory-fsync\",\n beforeRename: \"before-rename\",\n beforeStagingFsync: \"before-staging-fsync\",\n beforeStagingWrite: \"before-staging-write\",\n} as const;\n\nexport type DurableSessionFaultPoint =\n (typeof DURABLE_SESSION_FAULT_POINTS)[keyof typeof DURABLE_SESSION_FAULT_POINTS];\n\ntype FileIdentity = {\n dev: number;\n ino: number;\n};\n\ntype DirectoryIdentity = FileIdentity & {\n path: string;\n};\n\ntype SessionInventory = {\n archiveCount: number;\n totalBytes: number;\n};\n\nconst sameFile = (left: FileIdentity, right: FileIdentity): boolean =>\n left.dev === right.dev && left.ino === right.ino;\n\nconst assertOwner = (uid: number, label: string): void => {\n if (typeof process.getuid === \"function\" && uid !== process.getuid()) {\n throw new Error(`${label} must be owned by the current user`);\n }\n};\n\nconst assertPrivateMode = (mode: number, label: string): void => {\n if ((mode & 0o077) !== 0) {\n throw new Error(`${label} must not grant group or other permissions`);\n }\n};\n\nconst assertPosixDurabilitySupport = (): void => {\n if (\n (process.platform !== \"darwin\" && process.platform !== \"linux\") ||\n typeof process.getuid !== \"function\" ||\n typeof fsConstants.O_NOFOLLOW !== \"number\" ||\n fsConstants.O_NOFOLLOW === 0 ||\n typeof fsConstants.O_DIRECTORY !== \"number\" ||\n fsConstants.O_DIRECTORY === 0\n ) {\n throw new Error(\n \"Encrypted durable MCP sessions require supported POSIX owner, nofollow, directory-fsync, and advisory-lock semantics on macOS or Linux\",\n );\n }\n};\n\nconst acquireDirectoryLock = async (\n directoryPath: string,\n): Promise<FileHandle> => {\n const path = join(directoryPath, LOCK_FILE_NAME);\n const handle = await open(\n path,\n fsConstants.O_RDWR |\n fsConstants.O_CREAT |\n fsConstants.O_NOFOLLOW |\n fsConstants.O_NONBLOCK,\n 0o600,\n );\n try {\n const metadata = await handle.stat();\n if (!metadata.isFile()) {\n throw new Error(\"MCP session lock must be a regular file\");\n }\n assertOwner(metadata.uid, \"MCP session lock\");\n assertPrivateMode(metadata.mode, \"MCP session lock\");\n // Loaded lazily because Bun is used as a repository test/build tool but\n // cannot safely load this Node native addon. The shipped MCP runtime is\n // Node; Node integration tests exercise this path.\n const { tryLock } = await import(\"fs-native-extensions\");\n if (!tryLock(handle.fd)) {\n throw new Error(\n \"MCP session directory is already locked by another server\",\n );\n }\n return handle;\n } catch (error) {\n await handle.close().catch(() => undefined);\n throw error;\n }\n};\n\nconst canonicalAbsolutePath = async (\n path: string,\n label: string,\n): Promise<string> => {\n if (!isAbsolute(path)) {\n throw new Error(`${label} must be an absolute path`);\n }\n const normalized = resolve(path);\n const canonical = await realpath(normalized);\n if (canonical !== normalized) {\n throw new Error(`${label} must not contain symbolic links`);\n }\n return canonical;\n};\n\nconst readHandleBounded = async (\n handle: Pick<Awaited<ReturnType<typeof open>>, \"read\">,\n maximumBytes: number,\n): Promise<Uint8Array> => {\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const remaining = maximumBytes - total;\n const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, remaining + 1));\n const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null);\n if (bytesRead === 0) {\n return Buffer.concat(chunks, total);\n }\n total += bytesRead;\n if (total > maximumBytes) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n chunks.push(chunk.subarray(0, bytesRead));\n }\n};\n\nconst readKey = async (path: string): Promise<Uint8Array> => {\n const canonical = await canonicalAbsolutePath(path, \"MCP session key file\");\n const linkedMetadata = await lstat(canonical);\n if (!linkedMetadata.isFile() || linkedMetadata.isSymbolicLink()) {\n throw new Error(\"MCP session key file must be a regular file\");\n }\n assertOwner(linkedMetadata.uid, \"MCP session key file\");\n assertPrivateMode(linkedMetadata.mode, \"MCP session key file\");\n if (linkedMetadata.size !== SESSION_ARCHIVE_KEY_BYTES) {\n throw new Error(\n `MCP session key file must contain exactly ${SESSION_ARCHIVE_KEY_BYTES} raw bytes`,\n );\n }\n const handle = await open(\n canonical,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n let keyBuffer: Buffer | undefined;\n try {\n const openedMetadata = await handle.stat();\n if (\n !openedMetadata.isFile() ||\n !sameFile(linkedMetadata, openedMetadata) ||\n openedMetadata.size !== SESSION_ARCHIVE_KEY_BYTES\n ) {\n throw new Error(\"MCP session key file changed while it was validated\");\n }\n keyBuffer = Buffer.alloc(SESSION_ARCHIVE_KEY_BYTES + 1);\n let keyLength = 0;\n for (;;) {\n const { bytesRead } = await handle.read(\n keyBuffer,\n keyLength,\n keyBuffer.byteLength - keyLength,\n null,\n );\n if (bytesRead === 0 || keyLength + bytesRead === keyBuffer.byteLength) {\n keyLength += bytesRead;\n break;\n }\n keyLength += bytesRead;\n }\n if (keyLength !== SESSION_ARCHIVE_KEY_BYTES) {\n keyBuffer.fill(0);\n throw new Error(\n `MCP session key file must contain exactly ${SESSION_ARCHIVE_KEY_BYTES} raw bytes`,\n );\n }\n const currentMetadata = await stat(canonical);\n if (!sameFile(openedMetadata, currentMetadata)) {\n keyBuffer.fill(0);\n throw new Error(\"MCP session key file changed while it was read\");\n }\n const key = new Uint8Array(SESSION_ARCHIVE_KEY_BYTES);\n key.set(keyBuffer.subarray(0, SESSION_ARCHIVE_KEY_BYTES));\n keyBuffer.fill(0);\n return key;\n } finally {\n keyBuffer?.fill(0);\n await handle.close();\n }\n};\n\nconst archiveName = (sessionId: string): string =>\n `${createHash(\"sha256\").update(sessionId, \"utf8\").digest(\"hex\")}.stlasess`;\n\nconst assertSessionId = (sessionId: string): void => {\n if (!SESSION_ID.test(sessionId)) {\n throw new Error(\"MCP session ID is invalid\");\n }\n};\n\nexport type DurableSessionStoreOptions = {\n faultInjector?: (point: DurableSessionFaultPoint) => Promise<void> | void;\n keyFile: string;\n sessionDirectory: string;\n};\n\nexport type StoredSessionArchive = {\n bytes: Uint8Array;\n};\n\nexport type EncryptableSession = {\n toEncryptedArchiveAt(\n key: Uint8Array,\n observedAtEpochSeconds: number,\n ): Uint8Array;\n};\n\nexport type EncryptedSessionRestorer<Session> = {\n restoreEncryptedRedactionSession(options: {\n archive: Uint8Array;\n expectedSessionId: string;\n key: Uint8Array;\n observedAtEpochSeconds: number;\n }): Session;\n};\n\nexport type RestoreStoredSessionOptions<Session> = {\n archive: Uint8Array;\n expectedSessionId: string;\n observedAtEpochSeconds: number;\n restorer: EncryptedSessionRestorer<Session>;\n};\n\nexport class DurableSessionStore {\n #closePromise: Promise<void> | undefined;\n readonly #directory: DirectoryIdentity;\n readonly #faultInjector:\n | ((point: DurableSessionFaultPoint) => Promise<void> | void)\n | undefined;\n readonly #key: Uint8Array;\n readonly #lockHandle: FileHandle;\n #mutationTail: Promise<void> = Promise.resolve();\n #state: \"closed\" | \"closing\" | \"open\" = \"open\";\n\n private constructor(\n directory: DirectoryIdentity,\n key: Uint8Array,\n lockHandle: FileHandle,\n faultInjector?: (point: DurableSessionFaultPoint) => Promise<void> | void,\n ) {\n this.#directory = directory;\n this.#key = key;\n this.#lockHandle = lockHandle;\n this.#faultInjector = faultInjector;\n }\n\n static async create({\n keyFile,\n sessionDirectory,\n faultInjector,\n }: DurableSessionStoreOptions): Promise<DurableSessionStore> {\n assertPosixDurabilitySupport();\n const directoryPath = await canonicalAbsolutePath(\n sessionDirectory,\n \"MCP session directory\",\n );\n const metadata = await lstat(directoryPath);\n if (!metadata.isDirectory() || metadata.isSymbolicLink()) {\n throw new Error(\"MCP session directory must be a directory\");\n }\n assertOwner(metadata.uid, \"MCP session directory\");\n assertPrivateMode(metadata.mode, \"MCP session directory\");\n const key = await readKey(keyFile);\n let lockHandle: FileHandle | undefined;\n try {\n lockHandle = await acquireDirectoryLock(directoryPath);\n const store = new DurableSessionStore(\n { dev: metadata.dev, ino: metadata.ino, path: directoryPath },\n key,\n lockHandle,\n faultInjector,\n );\n await store.#validateInventory({ removeStagingFiles: true });\n await store.#syncDirectory();\n return store;\n } catch (error) {\n key.fill(0);\n await lockHandle?.close().catch(() => undefined);\n throw error;\n }\n }\n\n seal(\n session: EncryptableSession,\n observedAtEpochSeconds: number,\n ): Uint8Array {\n this.#assertOpen();\n return session.toEncryptedArchiveAt(this.#key, observedAtEpochSeconds);\n }\n\n restore<Session>({\n archive,\n expectedSessionId,\n observedAtEpochSeconds,\n restorer,\n }: RestoreStoredSessionOptions<Session>): Session {\n this.#assertOpen();\n return restorer.restoreEncryptedRedactionSession({\n archive,\n expectedSessionId,\n key: this.#key,\n observedAtEpochSeconds,\n });\n }\n\n async close(): Promise<void> {\n if (this.#closePromise !== undefined) {\n return this.#closePromise;\n }\n this.#state = \"closing\";\n this.#closePromise = (async () => {\n await this.#mutationTail;\n this.#key.fill(0);\n await this.#lockHandle.close();\n this.#state = \"closed\";\n })();\n return this.#closePromise;\n }\n\n async load(sessionId: string): Promise<StoredSessionArchive | undefined> {\n this.#assertOpen();\n return this.#withMutation(() => this.#load(sessionId));\n }\n\n async #load(sessionId: string): Promise<StoredSessionArchive | undefined> {\n assertSessionId(sessionId);\n await this.#validateInventory({ removeStagingFiles: false });\n const path = this.#path(sessionId);\n let handle: Awaited<ReturnType<typeof open>>;\n try {\n handle = await open(\n path,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw new Error(\"Encrypted session archive could not be opened\", {\n cause: error,\n });\n }\n try {\n const metadata = await handle.stat();\n if (!metadata.isFile()) {\n throw new Error(\"Encrypted session archive must be a regular file\");\n }\n assertOwner(metadata.uid, \"Encrypted session archive\");\n assertPrivateMode(metadata.mode, \"Encrypted session archive\");\n if (metadata.size > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n const bytes = await readHandleBounded(handle, SESSION_ARCHIVE_MAX_BYTES);\n await this.#assertDirectory();\n const currentMetadata = await lstat(path);\n if (!currentMetadata.isFile() || !sameFile(metadata, currentMetadata)) {\n throw new Error(\"Encrypted session archive changed while it was read\");\n }\n return { bytes };\n } finally {\n await handle.close();\n }\n }\n\n async save(sessionId: string, archive: Uint8Array): Promise<void> {\n this.#assertOpen();\n return this.#withMutation(() => this.#save(sessionId, archive));\n }\n\n async #save(sessionId: string, archive: Uint8Array): Promise<void> {\n assertSessionId(sessionId);\n if (archive.byteLength > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n const inventory = await this.#validateInventory({\n removeStagingFiles: false,\n });\n await this.#assertDirectory();\n const destination = this.#path(sessionId);\n const temporary = `${destination}.tmp.${randomUUID()}`;\n const handle = await open(\n temporary,\n fsConstants.O_WRONLY |\n fsConstants.O_CREAT |\n fsConstants.O_EXCL |\n fsConstants.O_NOFOLLOW,\n 0o600,\n );\n try {\n const openedMetadata = await handle.stat();\n if (!openedMetadata.isFile()) {\n throw new Error(\"Encrypted session staging path is not a regular file\");\n }\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeStagingWrite);\n await handle.writeFile(archive);\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeStagingFsync);\n await handle.sync();\n const stagedMetadata = await lstat(temporary);\n if (\n !stagedMetadata.isFile() ||\n !sameFile(openedMetadata, stagedMetadata)\n ) {\n throw new Error(\n \"Encrypted session staging file changed before publication\",\n );\n }\n await this.#assertDirectory();\n const existing = await lstat(destination).catch((error: unknown) => {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw error;\n });\n if (\n existing !== undefined &&\n (!existing.isFile() || existing.isSymbolicLink())\n ) {\n throw new Error(\"Encrypted session archive path is not a regular file\");\n }\n const nextCount =\n inventory.archiveCount + (existing === undefined ? 1 : 0);\n const nextTotalBytes =\n inventory.totalBytes - (existing?.size ?? 0) + archive.byteLength;\n if (nextCount > SESSION_ARCHIVE_MAX_COUNT) {\n throw new Error(\n `MCP session archives must not exceed ${SESSION_ARCHIVE_MAX_COUNT}`,\n );\n }\n if (nextTotalBytes > SESSION_ARCHIVE_TOTAL_MAX_BYTES) {\n throw new Error(\"MCP session archives exceed the aggregate byte limit\");\n }\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeRename);\n await rename(temporary, destination);\n await this.#assertDirectory();\n const published = await lstat(destination);\n if (!published.isFile() || !sameFile(openedMetadata, published)) {\n throw new Error(\"Encrypted session archive publication was not atomic\");\n }\n await this.#syncDirectory();\n } finally {\n await handle.close().catch(() => undefined);\n await unlink(temporary).catch(() => undefined);\n }\n }\n\n async delete(sessionId: string): Promise<void> {\n this.#assertOpen();\n return this.#withMutation(() => this.#delete(sessionId));\n }\n\n async #delete(sessionId: string): Promise<void> {\n assertSessionId(sessionId);\n await this.#assertDirectory();\n const path = this.#path(sessionId);\n const metadata = await lstat(path).catch((error: unknown) => {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return undefined;\n }\n throw error;\n });\n if (metadata === undefined) {\n return;\n }\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\"Encrypted session archive path is not a regular file\");\n }\n await unlink(path);\n await this.#syncDirectory();\n await this.#assertDirectory();\n }\n\n #path(sessionId: string): string {\n return join(this.#directory.path, archiveName(sessionId));\n }\n\n #assertOpen(): void {\n if (this.#state !== \"open\") {\n throw new Error(\"MCP durable session store is closing or closed\");\n }\n }\n\n async #withMutation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n const previous = this.#mutationTail;\n let release = (): void => undefined;\n const current = new Promise<void>((resolvePromise) => {\n release = resolvePromise;\n });\n this.#mutationTail = previous.then(() => current);\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n async #assertDirectory(): Promise<void> {\n const canonical = await realpath(this.#directory.path);\n const metadata = await lstat(this.#directory.path);\n if (\n canonical !== this.#directory.path ||\n !metadata.isDirectory() ||\n metadata.isSymbolicLink() ||\n !sameFile(this.#directory, metadata)\n ) {\n throw new Error(\"MCP session directory changed while it was being used\");\n }\n assertOwner(metadata.uid, \"MCP session directory\");\n assertPrivateMode(metadata.mode, \"MCP session directory\");\n }\n\n async #syncDirectory(): Promise<void> {\n await this.#inject(DURABLE_SESSION_FAULT_POINTS.beforeDirectoryFsync);\n const handle = await open(\n this.#directory.path,\n fsConstants.O_RDONLY | fsConstants.O_DIRECTORY | fsConstants.O_NOFOLLOW,\n );\n try {\n const metadata = await handle.stat();\n if (!metadata.isDirectory() || !sameFile(this.#directory, metadata)) {\n throw new Error(\"MCP session directory changed before synchronization\");\n }\n await handle.sync();\n } finally {\n await handle.close();\n }\n }\n\n async #inject(point: DurableSessionFaultPoint): Promise<void> {\n await this.#faultInjector?.(point);\n }\n\n async #validateInventory({\n removeStagingFiles,\n }: {\n removeStagingFiles: boolean;\n }): Promise<SessionInventory> {\n await this.#assertDirectory();\n const entries = await readdir(this.#directory.path, {\n withFileTypes: true,\n });\n let archiveCount = 0;\n let totalBytes = 0;\n for (const entry of entries) {\n const path = join(this.#directory.path, entry.name);\n if (entry.name === LOCK_FILE_NAME) {\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\"MCP session directory contains an unsafe lock path\");\n }\n assertOwner(metadata.uid, \"MCP session lock\");\n assertPrivateMode(metadata.mode, \"MCP session lock\");\n continue;\n }\n if (STAGING_NAME.test(entry.name)) {\n if (!removeStagingFiles) {\n throw new Error(\"MCP session directory contains a partial archive\");\n }\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\n \"MCP session directory contains an unsafe staging path\",\n );\n }\n await unlink(path);\n await this.#syncDirectory();\n continue;\n }\n if (\n !ARCHIVE_NAME.test(entry.name) ||\n !entry.isFile() ||\n entry.isSymbolicLink()\n ) {\n throw new Error(\"MCP session directory contains an unsupported entry\");\n }\n const metadata = await lstat(path);\n if (!metadata.isFile() || metadata.isSymbolicLink()) {\n throw new Error(\n \"MCP session directory contains an unsafe archive path\",\n );\n }\n assertOwner(metadata.uid, \"Encrypted session archive\");\n assertPrivateMode(metadata.mode, \"Encrypted session archive\");\n if (metadata.size > SESSION_ARCHIVE_MAX_BYTES) {\n throw new Error(\"Encrypted session archive exceeds the byte limit\");\n }\n archiveCount += 1;\n totalBytes += metadata.size;\n if (archiveCount > SESSION_ARCHIVE_MAX_COUNT) {\n throw new Error(\n `MCP session archives must not exceed ${SESSION_ARCHIVE_MAX_COUNT}`,\n );\n }\n if (totalBytes > SESSION_ARCHIVE_TOTAL_MAX_BYTES) {\n throw new Error(\"MCP session archives exceed the aggregate byte limit\");\n }\n }\n await this.#assertDirectory();\n return { archiveCount, totalBytes };\n }\n}\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport type { CallToolResult } from \"@modelcontextprotocol/sdk/types.js\";\nimport {\n CAPABILITY_MANIFEST,\n type NativeCallerDetection,\n type NativeTextReplacement,\n type PreparedNativePipeline,\n} from \"@stll/anonymize\";\nimport { preloadNativeBinding } from \"@stll/anonymize/native-runtime\";\nimport {\n AnonymizeSurfaceError,\n classifyToEnvelope,\n type AnonymizeErrorCode,\n type AnonymizeErrorEnvelope,\n} from \"@stll/anonymize/agent-surface\";\nimport {\n FEEDBACK_KINDS,\n MAX_FEEDBACK_BODY_CHARS,\n MAX_FEEDBACK_TITLE_CHARS,\n buildFeedbackSubmission,\n} from \"@stll/anonymize/feedback\";\nimport {\n DOCX_ARCHIVE_MAX_BYTES,\n DOCX_COVERAGE_MODES,\n anonymizeDocx,\n extractDocxText,\n restoreDocxText,\n type DocxAnonymizationSession,\n type DocxRestorationSession,\n} from \"@stll/anonymize-docx\";\nimport {\n PDF_DOCUMENT_MAX_BYTES,\n anonymizePdfRaster,\n renderPdfWithPopplerTesseract,\n} from \"@stll/anonymize-pdf\";\nimport * as nativeNode from \"@stll/anonymize/native-node\";\nimport {\n constants as fsConstants,\n link,\n lstat,\n open,\n realpath,\n stat,\n unlink,\n} from \"node:fs/promises\";\nimport { randomUUID } from \"node:crypto\";\nimport {\n dirname,\n extname,\n isAbsolute,\n relative,\n resolve,\n sep,\n} from \"node:path\";\nimport * as z from \"zod/v4\";\n\nimport pkg from \"../package.json\" with { type: \"json\" };\n\nimport { DurableSessionStore } from \"./durable-sessions\";\n\nconst TEXT_MAX_BYTES = 64 * 1024 * 1024;\nconst EXTERNAL_DETECTION_BATCH_MAX_BYTES = 16 * 1024 * 1024;\nconst EXTERNAL_DETECTION_BATCH_VERSION = 1 as const;\nconst PATH_MAX_CHARACTERS = 32_768;\nconst SESSION_MAX_COUNT = 256;\nconst SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;\nconst READ_CHUNK_BYTES = 64 * 1024;\n\n/** Build an agent-surface error carrying a stable code, message, and hint. */\nconst surfaceError = (\n code: AnonymizeErrorCode,\n message: string,\n hint: string,\n retryable = false,\n): AnonymizeSurfaceError =>\n new AnonymizeSurfaceError(code, message, { hint, retryable });\n\nconst nodeErrorCode = (error: unknown): string | undefined =>\n typeof error === \"object\" && error !== null && \"code\" in error\n ? (error as NodeJS.ErrnoException).code\n : undefined;\n\n/**\n * The local PDF provider collapses a missing/failed pdftoppm or tesseract into a\n * `PdfLocalProviderError` with code `executable-failed`; treat it as a missing\n * dependency so agents get an actionable install hint instead of a generic\n * internal error. Duck-typed to avoid importing the provider's internals.\n */\nconst isPdfToolchainUnavailable = (error: unknown): boolean =>\n typeof error === \"object\" &&\n error !== null &&\n (error as { name?: unknown }).name === \"PdfLocalProviderError\" &&\n (error as { code?: unknown }).code === \"executable-failed\";\n\n/**\n * The docx package refuses under requireFull coverage with a\n * `DocxAnonymizationError` (code `incomplete-coverage`). Map it to a\n * validation_error so the agent gets the actionable allowPartialCoverage hint\n * instead of a generic internal_error; pass anything else through unchanged.\n * Duck-typed to avoid importing the docx error class.\n */\nconst mapDocxCoverageError = (error: unknown): unknown => {\n if (\n typeof error === \"object\" &&\n error !== null &&\n (error as { name?: unknown }).name === \"DocxAnonymizationError\" &&\n (error as { code?: unknown }).code === \"incomplete-coverage\"\n ) {\n // Preserve the docx package's specific (content-free) coverage message; add\n // the stable code and the actionable hint.\n return new AnonymizeSurfaceError(\n \"validation_error\",\n (error as Error).message,\n {\n hint: \"Re-run with allowPartialCoverage: true to accept partial coverage.\",\n cause: error,\n },\n );\n }\n return error;\n};\n\nconst observedAtEpochSeconds = (): number => {\n const seconds = Math.floor(Date.now() / 1000);\n if (seconds < 0 || seconds > 0xff_ff_ff_ff) {\n throw new Error(\"The current time is outside the supported session range\");\n }\n return seconds;\n};\n\nexport const MCP_SESSION_MODES = {\n durableEncrypted: \"durable-encrypted\",\n memory: \"memory\",\n} as const;\n\nexport type McpSessionMode =\n (typeof MCP_SESSION_MODES)[keyof typeof MCP_SESSION_MODES];\n\nexport type AuditSafeResult = {\n operation: \"anonymize\" | \"inspect\" | \"restore\";\n format: \"docx\" | \"pdf\" | \"text\";\n outputCreated: boolean;\n sessionId?: string;\n entityCount?: number;\n blockCount?: number;\n rewrittenBlockCount?: number;\n restoredPlaceholderCount?: number;\n coverageStatus?: \"full\" | \"partial\";\n externalDetectionBatchStatus?: \"accepted\";\n externalDetectionCount?: number;\n retainedExternalDetectionCount?: number;\n pageCount?: number;\n mappedRegionCount?: number;\n structurePixelRewriteVerified?: true;\n piiCleanGuaranteed?: false;\n};\n\nconst EXTERNAL_DETECTION_FAILURES = {\n batchRejected: {\n code: \"EXTERNAL_DETECTION_BATCH_REJECTED\",\n message: \"The external detection batch was rejected.\",\n },\n documentRejected: {\n code: \"EXTERNAL_DETECTION_DOCUMENT_REJECTED\",\n message: \"The external detection document was rejected.\",\n },\n inputRejected: {\n code: \"EXTERNAL_DETECTION_INPUT_REJECTED\",\n message: \"The external detection request paths were rejected.\",\n },\n operationFailed: {\n code: \"EXTERNAL_DETECTION_OPERATION_FAILED\",\n message: \"The external detection operation failed safely.\",\n },\n sessionRejected: {\n code: \"EXTERNAL_DETECTION_SESSION_REJECTED\",\n message: \"The external detection session was rejected.\",\n },\n} as const;\n\ntype ExternalDetectionFailure =\n (typeof EXTERNAL_DETECTION_FAILURES)[keyof typeof EXTERNAL_DETECTION_FAILURES];\n\nclass ExternalDetectionAuditError extends Error {\n readonly code: ExternalDetectionFailure[\"code\"];\n\n constructor(failure: ExternalDetectionFailure) {\n super(failure.message);\n this.name = \"ExternalDetectionAuditError\";\n this.code = failure.code;\n }\n}\n\nconst externalDetectionFailure = (\n error: unknown,\n failure: ExternalDetectionFailure,\n): ExternalDetectionAuditError =>\n error instanceof ExternalDetectionAuditError\n ? error\n : new ExternalDetectionAuditError(failure);\n\nconst externalDetectionStep = async <Result>(\n failure: ExternalDetectionFailure,\n operation: () => Result | Promise<Result>,\n): Promise<Result> => {\n try {\n return await operation();\n } catch (error) {\n throw externalDetectionFailure(error, failure);\n }\n};\n\nexport type LocalAnonymizeServiceFaults = {\n beforeOutputPublish?: () => void | Promise<void>;\n};\n\nconst inside = (root: string, target: string): boolean => {\n const path = relative(root, target);\n return (\n path === \"\" ||\n (path !== \"..\" && !path.startsWith(`..${sep}`) && !isAbsolute(path))\n );\n};\n\ntype ReadInputOptions = {\n path: string;\n extension: \".docx\" | \".json\" | \".pdf\" | \".txt\";\n maximumBytes: number;\n label: \"DOCX\" | \"External detection batch\" | \"PDF\" | \"Text\";\n};\n\ntype ScopedInput = {\n bytes: Uint8Array;\n path: string;\n};\n\ntype ReadableFileHandle = Pick<Awaited<ReturnType<typeof open>>, \"read\">;\n\nconst readHandleBounded = async (\n handle: ReadableFileHandle,\n maximumBytes: number,\n label: \"DOCX\" | \"External detection batch\" | \"PDF\" | \"Text\",\n): Promise<Uint8Array> => {\n const chunks: Uint8Array[] = [];\n let total = 0;\n for (;;) {\n const chunk = Buffer.allocUnsafe(\n Math.min(READ_CHUNK_BYTES, maximumBytes - total + 1),\n );\n const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null);\n if (bytesRead === 0) {\n return Buffer.concat(chunks, total);\n }\n total += bytesRead;\n if (total > maximumBytes) {\n throw surfaceError(\n \"validation_error\",\n `${label} inputs must not exceed ${maximumBytes} bytes`,\n \"Split or shrink the input below the size limit and retry.\",\n );\n }\n chunks.push(chunk.subarray(0, bytesRead));\n }\n};\n\ntype DirectoryIdentity = {\n dev: number;\n ino: number;\n path: string;\n};\n\ntype FileIdentity = Pick<DirectoryIdentity, \"dev\" | \"ino\">;\n\nclass ScopedOutput {\n readonly parent: DirectoryIdentity;\n readonly path: string;\n\n constructor(path: string, parent: DirectoryIdentity) {\n this.path = path;\n this.parent = parent;\n }\n\n async write(bytes: Uint8Array | string): Promise<void> {\n await safeWrite(this, bytes);\n }\n}\n\nexport class PathScope {\n readonly #roots: readonly string[];\n\n private constructor(roots: readonly string[]) {\n this.#roots = roots;\n }\n\n static async create(roots: readonly string[]): Promise<PathScope> {\n if (roots.length === 0) {\n throw new Error(\"At least one --root directory is required\");\n }\n const canonical = await Promise.all(\n roots.map(async (root) => {\n if (!isAbsolute(root)) {\n throw new Error(\"MCP roots must be absolute paths\");\n }\n const path = await realpath(root);\n const metadata = await stat(path);\n if (!metadata.isDirectory()) {\n throw new Error(\"Every MCP root must be a directory\");\n }\n return path;\n }),\n );\n return new PathScope([...new Set(canonical)]);\n }\n\n async readInput({\n path,\n extension,\n maximumBytes,\n label,\n }: ReadInputOptions): Promise<ScopedInput> {\n if (!isAbsolute(path)) {\n throw surfaceError(\n \"validation_error\",\n `Input must be an absolute ${extension} path`,\n \"Pass an absolute path to an existing file inside a configured --root.\",\n );\n }\n if (extname(path).toLowerCase() !== extension) {\n throw surfaceError(\n \"unsupported_format\",\n `Input must be an absolute ${extension} path`,\n `Provide a file with the ${extension} extension.`,\n );\n }\n const initiallyCanonical = await this.canonicalInput(path);\n if (!this.#roots.some((root) => inside(root, initiallyCanonical))) {\n throw surfaceError(\n \"path_not_allowed\",\n \"Input is outside the configured roots\",\n \"Move the input under a configured --root, or add its directory with --root.\",\n );\n }\n const initiallyRequestedMetadata = await lstat(path);\n if (!initiallyRequestedMetadata.isFile()) {\n throw surfaceError(\n \"validation_error\",\n \"Input must be a regular file\",\n \"Point at a regular file, not a directory, symlink, or special file.\",\n );\n }\n const handle = await open(\n path,\n fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK,\n );\n try {\n const openedMetadata = await handle.stat();\n if (!openedMetadata.isFile()) {\n throw surfaceError(\n \"validation_error\",\n \"Input must be a regular file\",\n \"Point at a regular file, not a directory, symlink, or special file.\",\n );\n }\n const canonical = await realpath(path);\n if (!this.#roots.some((root) => inside(root, canonical))) {\n throw surfaceError(\n \"path_not_allowed\",\n \"Input is outside the configured roots\",\n \"Move the input under a configured --root, or add its directory with --root.\",\n );\n }\n const currentMetadata = await stat(canonical);\n if (\n currentMetadata.dev !== openedMetadata.dev ||\n currentMetadata.ino !== openedMetadata.ino\n ) {\n throw new Error(\"Input changed while it was being validated\");\n }\n if (openedMetadata.size > maximumBytes) {\n throw surfaceError(\n \"validation_error\",\n `${label} inputs must not exceed ${maximumBytes} bytes`,\n \"Split or shrink the input below the size limit and retry.\",\n );\n }\n const bytes = await readHandleBounded(handle, maximumBytes, label);\n return { bytes, path: canonical };\n } finally {\n await handle.close();\n }\n }\n\n /**\n * Canonicalize an input path, mapping a missing path to a `not_found` surface\n * error instead of a raw fs `ENOENT` so agents get a stable code.\n */\n private async canonicalInput(path: string): Promise<string> {\n try {\n return await realpath(path);\n } catch (error) {\n if (nodeErrorCode(error) === \"ENOENT\") {\n throw surfaceError(\n \"not_found\",\n \"Input path does not exist\",\n \"Create the file first, or pass an existing path inside a configured --root.\",\n );\n }\n throw error;\n }\n }\n\n async output(\n path: string,\n extension: \".docx\" | \".pdf\" | \".txt\",\n ): Promise<ScopedOutput> {\n if (!isAbsolute(path)) {\n throw surfaceError(\n \"validation_error\",\n `Output must be an absolute ${extension} path`,\n \"Pass an absolute output path inside a configured --root.\",\n );\n }\n if (extname(path).toLowerCase() !== extension) {\n throw surfaceError(\n \"unsupported_format\",\n `Output must be an absolute ${extension} path`,\n `Name the output with the ${extension} extension.`,\n );\n }\n const normalized = resolve(path);\n let parent: string;\n try {\n parent = await realpath(dirname(normalized));\n } catch (error) {\n if (nodeErrorCode(error) === \"ENOENT\") {\n throw surfaceError(\n \"not_found\",\n \"Output directory does not exist\",\n \"Create the output directory first, inside a configured --root.\",\n );\n }\n throw error;\n }\n if (!this.#roots.some((root) => inside(root, parent))) {\n throw surfaceError(\n \"path_not_allowed\",\n \"Output is outside the configured roots\",\n \"Choose an output directory under a configured --root.\",\n );\n }\n const parentMetadata = await stat(parent);\n try {\n await lstat(normalized);\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === \"ENOENT\") {\n return new ScopedOutput(normalized, {\n dev: parentMetadata.dev,\n ino: parentMetadata.ino,\n path: parent,\n });\n }\n throw new Error(\"Output availability could not be verified\", {\n cause: error,\n });\n }\n throw surfaceError(\n \"output_exists\",\n \"Output already exists; overwriting is not supported\",\n \"Pick a new output path; anonymize never overwrites an existing file.\",\n );\n }\n}\n\ntype SessionEntry = {\n language: string | undefined;\n session: RedactionSession;\n restoreSession: (plaintextJson: string) => RedactionSession;\n status: \"busy\" | \"initializing\" | \"ready\";\n};\n\ntype SessionLease = {\n entry: SessionEntry;\n sessionId: string;\n rollback:\n | { type: \"delete\" }\n | { type: \"release\" }\n | { type: \"restore\"; checkpoint: string };\n};\n\ntype RedactionSession = DocxAnonymizationSession &\n DocxRestorationSession & {\n redact_text(text: string): {\n redaction: { redactedText: string; entityCount: number };\n };\n restoreText(text: string): string;\n toPlaintextJson(): string;\n toEncryptedArchiveAt(\n key: Uint8Array,\n observedAtEpochSeconds: number,\n ): Uint8Array;\n };\n\ntype NativeNodeSurface = {\n convert_external_detection_batch: (\n document: Uint8Array,\n batch: string,\n ) => NativeCallerDetection[];\n getDefaultNativePipeline: (options: { language?: string }) => {\n createRedactionSession: (sessionId: string) => RedactionSession;\n restoreRedactionSession: (plaintextJson: string) => RedactionSession;\n restoreEncryptedRedactionSession: (options: {\n archive: Uint8Array;\n expectedSessionId: string;\n key: Uint8Array;\n observedAtEpochSeconds: number;\n }) => RedactionSession;\n } & Pick<\n PreparedNativePipeline,\n \"redactText\" | \"redactTextWithCallerDetections\"\n >;\n native_package_version: () => string;\n};\n\nconst nativeNodeSurface = nativeNode as unknown as NativeNodeSurface; // SAFETY: These native-node runtime exports are public, but their generated declarations are minified during workspace builds.\n\nconst assertOutputParent = async ({\n parent,\n path,\n}: ScopedOutput): Promise<void> => {\n const canonical = await realpath(dirname(path));\n const metadata = await stat(canonical);\n if (\n canonical !== parent.path ||\n metadata.dev !== parent.dev ||\n metadata.ino !== parent.ino\n ) {\n throw new Error(\"Output directory changed while it was being used\");\n }\n};\n\nconst sameFile = (left: FileIdentity, right: FileIdentity): boolean =>\n left.dev === right.dev && left.ino === right.ino;\n\nconst safeWrite = async (\n output: ScopedOutput,\n bytes: Uint8Array | string,\n): Promise<void> => {\n await assertOutputParent(output);\n const temporary = `${output.path}.stella-${randomUUID()}.tmp`;\n let published = false;\n let committed = false;\n let temporaryMetadata: FileIdentity | undefined;\n const handle = await open(\n temporary,\n fsConstants.O_WRONLY |\n fsConstants.O_CREAT |\n fsConstants.O_EXCL |\n fsConstants.O_NOFOLLOW,\n 0o400,\n );\n try {\n temporaryMetadata = await handle.stat();\n const canonicalTemporary = await realpath(temporary);\n const currentTemporaryMetadata = await lstat(temporary);\n if (\n dirname(canonicalTemporary) !== output.parent.path ||\n !currentTemporaryMetadata.isFile() ||\n !sameFile(temporaryMetadata, currentTemporaryMetadata)\n ) {\n throw new Error(\"Output staging file changed while it was being used\");\n }\n await handle.writeFile(bytes);\n await handle.sync();\n await assertOutputParent(output);\n const stagedMetadata = await lstat(temporary);\n if (\n !stagedMetadata.isFile() ||\n !sameFile(temporaryMetadata, stagedMetadata)\n ) {\n throw new Error(\"Output staging file changed before publication\");\n }\n await link(temporary, output.path);\n published = true;\n const publishedMetadata = await lstat(output.path);\n if (\n !publishedMetadata.isFile() ||\n !sameFile(temporaryMetadata, publishedMetadata)\n ) {\n throw new Error(\"Published output does not match the staged file\");\n }\n await assertOutputParent(output);\n await handle.chmod(0o600);\n await handle.sync();\n committed = true;\n } finally {\n if (!committed) {\n await handle.truncate(0).catch(() => undefined);\n await handle.sync().catch(() => undefined);\n await handle.chmod(0o000).catch(() => undefined);\n }\n await handle.close().catch(() => undefined);\n await unlink(temporary).catch(() => undefined);\n if (published && !committed && temporaryMetadata !== undefined) {\n const publishedMetadata = await lstat(output.path).catch(() => undefined);\n if (\n publishedMetadata !== undefined &&\n sameFile(temporaryMetadata, publishedMetadata)\n ) {\n await unlink(output.path).catch(() => undefined);\n }\n }\n }\n};\n\nconst decodeText = (bytes: Uint8Array): string => {\n return decodeUtf8(bytes, \"Text inputs\");\n};\n\nconst decodeUtf8 = (bytes: Uint8Array, label: string): string => {\n try {\n return new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n } catch (error) {\n throw new AnonymizeSurfaceError(\n \"validation_error\",\n `${label} must contain valid UTF-8`,\n {\n hint: \"Provide UTF-8 encoded text; re-encode the file and retry.\",\n cause: error,\n },\n );\n }\n};\n\nconst applyTextReplacements = (\n text: string,\n replacements: readonly NativeTextReplacement[],\n): string => {\n const parts: string[] = [];\n let cursor = 0;\n for (const replacement of replacements) {\n if (\n !Number.isSafeInteger(replacement.start) ||\n !Number.isSafeInteger(replacement.end) ||\n replacement.start < cursor ||\n replacement.end <= replacement.start ||\n replacement.end > text.length ||\n !isUtf16Boundary(text, replacement.start) ||\n !isUtf16Boundary(text, replacement.end)\n ) {\n throw new Error(\n \"Native caller-detection plan returned invalid replacements\",\n );\n }\n parts.push(text.slice(cursor, replacement.start), replacement.replacement);\n cursor = replacement.end;\n }\n parts.push(text.slice(cursor));\n return parts.join(\"\");\n};\n\nconst isUtf16Boundary = (text: string, offset: number): boolean => {\n if (offset <= 0 || offset >= text.length) {\n return true;\n }\n const before = text.charCodeAt(offset - 1);\n const after = text.charCodeAt(offset);\n return !(\n before >= 0xd800 &&\n before <= 0xdbff &&\n after >= 0xdc00 &&\n after <= 0xdfff\n );\n};\n\nconst assertDifferentPaths = (input: string, output: string): void => {\n if (input === output) {\n throw surfaceError(\n \"validation_error\",\n \"Input and output paths must differ\",\n \"Choose a distinct output path so the input is never overwritten.\",\n );\n }\n};\n\nconst textInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n sessionId: z.string().regex(SESSION_ID),\n language: z.string().min(2).max(35).optional(),\n});\n\nconst externalDetectionTextInput = textInput.extend({\n detectionBatchPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n});\n\nconst restoreInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n sessionId: z.string().regex(SESSION_ID),\n});\n\nconst docxRestoreInput = restoreInput.extend({\n allowPartialCoverage: z.boolean().optional().default(false),\n});\n\nconst docxInput = textInput.extend({\n allowPartialCoverage: z.boolean().optional().default(false),\n});\n\nconst pdfInput = z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n outputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n ocrLanguage: z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_.-]*$/u),\n detectionLanguage: z.string().min(2).max(35).optional(),\n dpi: z.number().int().min(72).max(600).optional().default(300),\n timeoutMs: z.number().int().min(100).max(300_000).optional().default(120_000),\n fillRgb: z\n .tuple([\n z.number().int().min(0).max(255),\n z.number().int().min(0).max(255),\n z.number().int().min(0).max(255),\n ])\n .optional()\n .default([0, 0, 0]),\n});\n\nexport type LocalPdfProviderConfiguration = {\n pdftoppmPath?: string | undefined;\n tesseractPath?: string | undefined;\n};\n\nexport type LocalAnonymizeServiceOptions = {\n durableSessions?: DurableSessionStore | undefined;\n faults?: LocalAnonymizeServiceFaults | undefined;\n pdfProvider?: LocalPdfProviderConfiguration | undefined;\n};\n\nexport class LocalAnonymizeService {\n #activeOperations = 0;\n #closePromise: Promise<void> | undefined;\n #operationsDrained: (() => void) | undefined;\n readonly #scope: PathScope;\n #pdfOperationTail: Promise<void> = Promise.resolve();\n readonly #sessionInitializations = new Set<string>();\n readonly #sessions = new Map<string, SessionEntry>();\n #state: \"closed\" | \"closing\" | \"open\" = \"open\";\n readonly #durableSessions: DurableSessionStore | undefined;\n readonly #faults: LocalAnonymizeServiceFaults;\n readonly #pdfProvider: LocalPdfProviderConfiguration;\n\n constructor(scope: PathScope, options: LocalAnonymizeServiceOptions = {}) {\n if (options instanceof DurableSessionStore) {\n throw new TypeError(\n \"LocalAnonymizeService requires { durableSessions } as its second argument\",\n );\n }\n const { durableSessions, faults = {}, pdfProvider = {} } = options;\n this.#scope = scope;\n this.#durableSessions = durableSessions;\n this.#faults = faults;\n this.#pdfProvider = pdfProvider;\n }\n\n get sessionMode(): McpSessionMode {\n return this.#durableSessions === undefined\n ? MCP_SESSION_MODES.memory\n : MCP_SESSION_MODES.durableEncrypted;\n }\n\n async close(): Promise<void> {\n if (this.#closePromise !== undefined) {\n return this.#closePromise;\n }\n this.#state = \"closing\";\n this.#closePromise = (async () => {\n if (this.#activeOperations > 0) {\n await new Promise<void>((resolvePromise) => {\n this.#operationsDrained = resolvePromise;\n });\n }\n this.#sessions.clear();\n await this.#durableSessions?.close();\n this.#state = \"closed\";\n })();\n return this.#closePromise;\n }\n\n async #runOperation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#state !== \"open\") {\n throw new Error(\"MCP anonymize service is closing or closed\");\n }\n this.#activeOperations += 1;\n try {\n // Count the operation before the asynchronous preload so close() cannot\n // pass its drain barrier while this call is waiting for the binding.\n // The preload installs wasm under Bun before any inline native load in\n // the docx or pdf package; it is a no-op on Node.\n await preloadNativeBinding();\n return await operation();\n } finally {\n this.#activeOperations -= 1;\n if (this.#activeOperations === 0) {\n this.#operationsDrained?.();\n this.#operationsDrained = undefined;\n }\n }\n }\n\n async #session(sessionId: string, language?: string): Promise<SessionLease> {\n if (this.#durableSessions !== undefined && language !== undefined) {\n throw new Error(\n \"Durable sessions use the full all-language pipeline; omit language\",\n );\n }\n const existing = this.#sessions.get(sessionId);\n if (existing !== undefined) {\n if (language !== undefined && existing.language !== language) {\n throw surfaceError(\n \"validation_error\",\n \"A session cannot change language\",\n \"Use a new session id for a different language, or drop the language override.\",\n );\n }\n if (existing.status === \"initializing\") {\n throw new Error(\"The requested session is still initializing\");\n }\n if (existing.status === \"busy\") {\n throw new Error(\"The requested session is handling another operation\");\n }\n const checkpoint = existing.session.toPlaintextJson();\n existing.status = \"busy\";\n return {\n entry: existing,\n sessionId,\n rollback: { type: \"restore\", checkpoint },\n };\n }\n if (this.#sessionInitializations.has(sessionId)) {\n throw new Error(\"The requested session is still initializing\");\n }\n this.#sessionInitializations.add(sessionId);\n try {\n if (this.#sessions.size >= SESSION_MAX_COUNT) {\n throw new Error(`MCP sessions must not exceed ${SESSION_MAX_COUNT}`);\n }\n const pipeline = nativeNodeSurface.getDefaultNativePipeline(\n language === undefined ? {} : { language },\n );\n const durableSessions = this.#durableSessions;\n const stored =\n durableSessions === undefined\n ? undefined\n : await durableSessions.load(sessionId);\n let session: RedactionSession;\n if (stored === undefined) {\n session = pipeline.createRedactionSession(sessionId);\n } else {\n if (durableSessions === undefined) {\n throw surfaceError(\n \"session_unavailable\",\n \"Durable session storage is unavailable\",\n \"Start the server with --session-dir and --key-file to enable restores.\",\n );\n }\n try {\n session = durableSessions.restore({\n archive: stored.bytes,\n expectedSessionId: sessionId,\n observedAtEpochSeconds: observedAtEpochSeconds(),\n restorer: pipeline,\n });\n } catch (error) {\n throw new AnonymizeSurfaceError(\n \"session_unavailable\",\n \"The requested durable session is unavailable\",\n {\n hint: \"Confirm the session id and key file match the archive that created it.\",\n cause: error,\n },\n );\n }\n }\n const entry: SessionEntry = {\n language,\n session,\n restoreSession: (plaintextJson) =>\n pipeline.restoreRedactionSession(plaintextJson),\n status: \"initializing\",\n };\n this.#sessions.set(sessionId, entry);\n return {\n entry,\n sessionId,\n rollback:\n stored === undefined\n ? { type: \"delete\" }\n : { type: \"restore\", checkpoint: session.toPlaintextJson() },\n };\n } finally {\n this.#sessionInitializations.delete(sessionId);\n }\n }\n\n #commitSession({ entry }: SessionLease): void {\n entry.status = \"ready\";\n }\n\n async #rollbackSession({\n entry,\n rollback,\n sessionId,\n }: SessionLease): Promise<void> {\n if (this.#sessions.get(sessionId) !== entry) {\n return;\n }\n if (rollback.type === \"delete\") {\n this.#sessions.delete(sessionId);\n await this.#durableSessions?.delete(sessionId).catch(() => undefined);\n return;\n }\n if (rollback.type === \"release\") {\n entry.status = \"ready\";\n return;\n }\n try {\n entry.session = entry.restoreSession(rollback.checkpoint);\n entry.status = \"ready\";\n await this.#persistSession(sessionId, entry.session);\n } catch {\n this.#sessions.delete(sessionId);\n }\n }\n\n async #readSession(sessionId: string): Promise<SessionLease> {\n let entry = this.#sessions.get(sessionId);\n if (entry === undefined && this.#durableSessions !== undefined) {\n if (this.#sessionInitializations.has(sessionId)) {\n throw new Error(\"The requested session is still initializing\");\n }\n this.#sessionInitializations.add(sessionId);\n try {\n const pipeline = nativeNodeSurface.getDefaultNativePipeline({});\n const stored = await this.#durableSessions.load(sessionId);\n if (stored !== undefined) {\n try {\n const session = this.#durableSessions.restore({\n archive: stored.bytes,\n expectedSessionId: sessionId,\n observedAtEpochSeconds: observedAtEpochSeconds(),\n restorer: pipeline,\n });\n entry = {\n language: undefined,\n session,\n restoreSession: (plaintextJson) =>\n pipeline.restoreRedactionSession(plaintextJson),\n status: \"ready\",\n };\n this.#sessions.set(sessionId, entry);\n } catch (error) {\n throw new AnonymizeSurfaceError(\n \"session_unavailable\",\n \"The requested durable session is unavailable\",\n {\n hint: \"Confirm the session id and key file match the archive that created it.\",\n cause: error,\n },\n );\n }\n }\n } finally {\n this.#sessionInitializations.delete(sessionId);\n }\n }\n if (entry === undefined) {\n throw surfaceError(\n \"session_unavailable\",\n \"The requested session is unavailable\",\n \"Anonymize with this session id first, or start the server with a durable session store.\",\n );\n }\n if (entry.status !== \"ready\") {\n throw surfaceError(\n \"session_unavailable\",\n \"The requested in-memory session is unavailable\",\n \"Wait for the prior operation on this session to finish, then retry.\",\n true,\n );\n }\n entry.status = \"busy\";\n return { entry, sessionId, rollback: { type: \"release\" } };\n }\n\n async #persistSession(\n sessionId: string,\n session: RedactionSession,\n ): Promise<void> {\n if (this.#durableSessions === undefined) {\n return;\n }\n const archive = this.#durableSessions.seal(\n session,\n observedAtEpochSeconds(),\n );\n await this.#durableSessions.save(sessionId, archive);\n }\n\n async anonymizeText(\n input: z.infer<typeof textInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#anonymizeText(input));\n }\n\n async anonymizePdf(\n input: z.infer<typeof pdfInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() =>\n this.#serializePdfOperation(() => this.#anonymizePdf(input)),\n );\n }\n\n async #serializePdfOperation<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n const previous = this.#pdfOperationTail;\n let release = (): void => undefined;\n this.#pdfOperationTail = new Promise<void>((resolvePromise) => {\n release = resolvePromise;\n });\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n async #anonymizePdf(\n input: z.infer<typeof pdfInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".pdf\",\n maximumBytes: PDF_DOCUMENT_MAX_BYTES,\n label: \"PDF\",\n });\n const destination = await this.#scope.output(input.outputPath, \".pdf\");\n assertDifferentPaths(source.path, destination.path);\n const pipeline = nativeNodeSurface.getDefaultNativePipeline(\n input.detectionLanguage === undefined\n ? {}\n : { language: input.detectionLanguage },\n );\n let observed: Awaited<ReturnType<typeof renderPdfWithPopplerTesseract>>;\n try {\n observed = await renderPdfWithPopplerTesseract({\n document: source.bytes,\n ocrLanguage: input.ocrLanguage,\n dpi: input.dpi,\n timeoutMs: input.timeoutMs,\n ...this.#pdfProvider,\n });\n } catch (error) {\n if (isPdfToolchainUnavailable(error)) {\n throw new AnonymizeSurfaceError(\n \"dependency_missing\",\n \"The local PDF toolchain is unavailable\",\n {\n hint: \"Install Poppler (pdftoppm) and Tesseract on PATH, or pass --pdftoppm/--tesseract.\",\n cause: error,\n },\n );\n }\n throw error;\n }\n const anonymized = anonymizePdfRaster({\n document: source.bytes,\n pipeline,\n provider: observed.provider,\n pages: observed.pages,\n fillRgb: input.fillRgb,\n });\n if (\n anonymized.certificate.structurePixelRewriteVerified !== true ||\n anonymized.certificate.piiCleanGuaranteed !== false\n ) {\n throw new Error(\"PDF raster verification did not satisfy MCP policy\");\n }\n await this.#faults.beforeOutputPublish?.();\n await destination.write(anonymized.document);\n return {\n operation: \"anonymize\",\n format: \"pdf\",\n outputCreated: true,\n pageCount: anonymized.certificate.pageCount,\n entityCount: anonymized.certificate.detectionCount,\n mappedRegionCount: anonymized.certificate.mappedRegionCount,\n structurePixelRewriteVerified: true,\n piiCleanGuaranteed: false,\n };\n }\n\n async #anonymizeText(\n input: z.infer<typeof textInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const destination = await this.#scope.output(input.outputPath, \".txt\");\n assertDifferentPaths(source.path, destination.path);\n const text = decodeText(source.bytes);\n const lease = await this.#session(input.sessionId, input.language);\n try {\n const result = lease.entry.session.redact_text(text);\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(result.redaction.redactedText);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: result.redaction.entityCount,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async restoreText(\n input: z.infer<typeof restoreInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#restoreText(input));\n }\n\n async anonymizeTextWithExternalDetections(\n input: z.infer<typeof externalDetectionTextInput>,\n ): Promise<AuditSafeResult> {\n try {\n return await this.#runOperation(() =>\n this.#anonymizeTextWithExternalDetections(input),\n );\n } catch (error) {\n throw externalDetectionFailure(\n error,\n EXTERNAL_DETECTION_FAILURES.operationFailed,\n );\n }\n }\n\n async #anonymizeTextWithExternalDetections(\n input: z.infer<typeof externalDetectionTextInput>,\n ): Promise<AuditSafeResult> {\n const { batch, destination, source } = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.inputRejected,\n async () => {\n const scopedSource = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const scopedBatch = await this.#scope.readInput({\n path: input.detectionBatchPath,\n extension: \".json\",\n maximumBytes: EXTERNAL_DETECTION_BATCH_MAX_BYTES,\n label: \"External detection batch\",\n });\n const scopedDestination = await this.#scope.output(\n input.outputPath,\n \".txt\",\n );\n assertDifferentPaths(scopedSource.path, scopedDestination.path);\n assertDifferentPaths(scopedBatch.path, scopedDestination.path);\n assertDifferentPaths(scopedSource.path, scopedBatch.path);\n return {\n batch: scopedBatch,\n destination: scopedDestination,\n source: scopedSource,\n };\n },\n );\n const text = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.documentRejected,\n () => decodeText(source.bytes),\n );\n const detections = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.batchRejected,\n () =>\n nativeNodeSurface.convert_external_detection_batch(\n source.bytes,\n decodeUtf8(batch.bytes, \"External detection batches\"),\n ),\n );\n const lease = await externalDetectionStep(\n EXTERNAL_DETECTION_FAILURES.sessionRejected,\n () => this.#session(input.sessionId, input.language),\n );\n try {\n const plan = lease.entry.session.planTextBatchWithCallerDetections({\n inputs: [{ fullText: text, detections }],\n });\n const block = plan.blocks.at(0);\n if (plan.blocks.length !== 1 || block === undefined) {\n throw new Error(\n \"Native caller-detection plan did not match the text input\",\n );\n }\n const redactedText = applyTextReplacements(text, block.replacements);\n plan.commit();\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(redactedText);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: block.entityCount,\n externalDetectionBatchStatus: \"accepted\",\n externalDetectionCount: detections.length,\n retainedExternalDetectionCount: block.callerEntityCount,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw externalDetectionFailure(\n error,\n EXTERNAL_DETECTION_FAILURES.operationFailed,\n );\n }\n }\n\n async #restoreText(\n input: z.infer<typeof restoreInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".txt\",\n maximumBytes: TEXT_MAX_BYTES,\n label: \"Text\",\n });\n const destination = await this.#scope.output(input.outputPath, \".txt\");\n assertDifferentPaths(source.path, destination.path);\n const text = decodeText(source.bytes);\n const lease = await this.#readSession(input.sessionId);\n try {\n const restored = lease.entry.session.restoreText(text);\n await destination.write(restored);\n this.#commitSession(lease);\n return {\n operation: \"restore\",\n format: \"text\",\n outputCreated: true,\n sessionId: input.sessionId,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async anonymizeDocx(\n input: z.infer<typeof docxInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#anonymizeDocx(input));\n }\n\n async #anonymizeDocx(\n input: z.infer<typeof docxInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const destination = await this.#scope.output(input.outputPath, \".docx\");\n assertDifferentPaths(source.path, destination.path);\n const lease = await this.#session(input.sessionId, input.language);\n try {\n const result = anonymizeDocx({\n document: source.bytes,\n session: lease.entry.session,\n expectedSessionId: input.sessionId,\n policy: {\n coverage: {\n mode: input.allowPartialCoverage\n ? DOCX_COVERAGE_MODES.allowPartial\n : DOCX_COVERAGE_MODES.requireFull,\n },\n },\n });\n await this.#persistSession(input.sessionId, lease.entry.session);\n await this.#faults.beforeOutputPublish?.();\n await destination.write(result.document);\n this.#commitSession(lease);\n return {\n operation: \"anonymize\",\n format: \"docx\",\n outputCreated: true,\n sessionId: input.sessionId,\n entityCount: result.summary.entityCount,\n blockCount: result.summary.blockCount,\n rewrittenBlockCount: result.summary.rewrittenBlockCount,\n coverageStatus: result.summary.coverage.status,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw mapDocxCoverageError(error);\n }\n }\n\n async restoreDocx(\n input: z.infer<typeof docxRestoreInput>,\n ): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#restoreDocx(input));\n }\n\n async #restoreDocx(\n input: z.infer<typeof docxRestoreInput>,\n ): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: input.inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const destination = await this.#scope.output(input.outputPath, \".docx\");\n assertDifferentPaths(source.path, destination.path);\n const lease = await this.#readSession(input.sessionId);\n try {\n const result = restoreDocxText({\n document: source.bytes,\n session: lease.entry.session,\n expectedSessionId: input.sessionId,\n });\n if (result.coverage.status === \"partial\" && !input.allowPartialCoverage) {\n throw surfaceError(\n \"validation_error\",\n \"DOCX restoration has partial coverage; set allowPartialCoverage to publish it\",\n \"Re-run with allowPartialCoverage: true to accept partial restoration.\",\n );\n }\n await destination.write(result.document);\n this.#commitSession(lease);\n return {\n operation: \"restore\",\n format: \"docx\",\n outputCreated: true,\n sessionId: input.sessionId,\n rewrittenBlockCount: result.restoredBlockCount,\n restoredPlaceholderCount: result.restoredPlaceholderCount,\n coverageStatus: result.coverage.status,\n };\n } catch (error) {\n await this.#rollbackSession(lease);\n throw error;\n }\n }\n\n async inspectDocx(inputPath: string): Promise<AuditSafeResult> {\n return this.#runOperation(() => this.#inspectDocx(inputPath));\n }\n\n async #inspectDocx(inputPath: string): Promise<AuditSafeResult> {\n const source = await this.#scope.readInput({\n path: inputPath,\n extension: \".docx\",\n maximumBytes: DOCX_ARCHIVE_MAX_BYTES,\n label: \"DOCX\",\n });\n const extraction = extractDocxText(source.bytes);\n const unsupported = extraction.coverage.parts.some(\n (part) => part.status === \"unsupported\",\n );\n const structuralGap =\n extraction.coverage.hyperlinkTextSegmentCount > 0 ||\n extraction.coverage.revisionTextSegmentCount > 0 ||\n extraction.coverage.unsupportedAlternateContentCount > 0 ||\n extraction.coverage.unsupportedFieldInstructionCount > 0 ||\n extraction.coverage.unsupportedSymbolCount > 0;\n return {\n operation: \"inspect\",\n format: \"docx\",\n outputCreated: false,\n blockCount: extraction.blocks.length,\n coverageStatus: unsupported || structuralGap ? \"partial\" : \"full\",\n };\n }\n}\n\nconst result = (value: AuditSafeResult): CallToolResult => ({\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: { ...value },\n});\n\nconst MCP_TOOL_NAMES = [\n \"anonymize_docx_file\",\n \"anonymize_pdf_file\",\n \"anonymize_text_file\",\n \"anonymize_text_file_with_external_detections\",\n \"capabilities\",\n \"inspect_docx_file\",\n \"restore_docx_file\",\n \"restore_text_file\",\n \"send_feedback\",\n] as const;\n\n/**\n * Map the external-detection failure taxonomy onto the shared agent-surface\n * codes. The specific failure identity is preserved in the envelope `message`;\n * `code` gives the agent the coarse, branchable class.\n */\nconst EXTERNAL_DETECTION_ENVELOPE: Record<\n ExternalDetectionFailure[\"code\"],\n { code: AnonymizeErrorCode; hint: string; retryable: boolean }\n> = {\n EXTERNAL_DETECTION_BATCH_REJECTED: {\n code: \"validation_error\",\n hint: \"Fix the ExternalDetectionBatch v1 sidecar to match the schema and retry.\",\n retryable: false,\n },\n EXTERNAL_DETECTION_DOCUMENT_REJECTED: {\n code: \"validation_error\",\n hint: \"Align the sidecar's document metadata with the input, then retry.\",\n retryable: false,\n },\n EXTERNAL_DETECTION_INPUT_REJECTED: {\n code: \"validation_error\",\n hint: \"Use distinct absolute paths inside a configured --root for input, sidecar, and output.\",\n retryable: false,\n },\n EXTERNAL_DETECTION_OPERATION_FAILED: {\n code: \"internal_error\",\n hint: \"Retry; if it persists, file it with the send_feedback tool.\",\n retryable: true,\n },\n EXTERNAL_DETECTION_SESSION_REJECTED: {\n code: \"session_unavailable\",\n hint: \"Use a fresh session id, or confirm the session store and key file match.\",\n retryable: false,\n },\n};\n\nconst toEnvelope = (error: unknown): AnonymizeErrorEnvelope => {\n if (error instanceof ExternalDetectionAuditError) {\n const mapped = EXTERNAL_DETECTION_ENVELOPE[error.code];\n return {\n error: {\n code: mapped.code,\n message: error.message,\n hint: mapped.hint,\n retryable: mapped.retryable,\n },\n };\n }\n return classifyToEnvelope(error);\n};\n\nconst errorResult = (error: unknown): CallToolResult => {\n const envelope = toEnvelope(error);\n return {\n isError: true,\n content: [{ type: \"text\" as const, text: JSON.stringify(envelope) }],\n structuredContent: { ...envelope },\n };\n};\n\n/**\n * Run a tool body, rendering any thrown error as the structured envelope so\n * every tool fails the same, agent-legible way instead of surfacing a raw\n * protocol error.\n */\nconst guard = async (\n produce: () => CallToolResult | Promise<CallToolResult>,\n): Promise<CallToolResult> => {\n try {\n return await produce();\n } catch (error) {\n return errorResult(error);\n }\n};\n\nconst capabilitiesResult = async (\n service: LocalAnonymizeService,\n): Promise<CallToolResult> => {\n await preloadNativeBinding();\n const value = {\n capabilityManifest: CAPABILITY_MANIFEST,\n runtimeVersion: nativeNodeSurface.native_package_version(),\n mcp: {\n externalDetectionBatch: {\n ingestion: \"path-only\" as const,\n version: EXTERNAL_DETECTION_BATCH_VERSION,\n },\n formats: [\"docx\", \"pdf\", \"text\"] as const,\n sessionMode: service.sessionMode,\n tools: MCP_TOOL_NAMES,\n transport: \"stdio\" as const,\n },\n };\n return {\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: value,\n };\n};\n\nconst feedbackInput = z.object({\n kind: z.enum(FEEDBACK_KINDS),\n title: z.string().min(1).max(MAX_FEEDBACK_TITLE_CHARS),\n body: z.string().min(1).max(MAX_FEEDBACK_BODY_CHARS),\n});\n\nconst feedbackResult = (\n input: z.infer<typeof feedbackInput>,\n): CallToolResult => {\n const submission = buildFeedbackSubmission(input);\n const value = {\n channel: \"github\" as const,\n redactions: submission.redactions,\n title: submission.title,\n sanitizedBody: submission.sanitizedBody,\n issueUrl: submission.issueUrl,\n ghCommand: submission.ghCommand,\n note: \"Nothing was sent. Review the sanitized content, then open the URL (or run the gh command) to submit the issue under your own GitHub account.\",\n };\n return {\n content: [{ type: \"text\" as const, text: JSON.stringify(value) }],\n structuredContent: { ...value },\n };\n};\n\n/**\n * Server `instructions` handed to MCP clients at connect time. Kept terse and\n * factual; the char budget is asserted in `instructions.test.ts` to guard drift.\n */\nexport const MCP_INSTRUCTIONS_MAX_CHARS = 1200;\nexport const MCP_INSTRUCTIONS = `stella-anonymize redacts PII in local text, DOCX, and PDF files. Every tool reads and writes local paths only, inside the directories passed as --root; it never returns document text or session mappings, and it never overwrites, so outputs must be new paths.\n\nErrors: a failed tool returns a single text content of {\"error\":{\"code\",\"message\",\"hint\",\"retryable\"}} with isError set. Branch on code (validation_error, path_not_allowed, not_found, unsupported_format, output_exists, session_unavailable, dependency_missing, internal_error); hint states the next step. Nothing here is destructive: there is no delete and existing files are never overwritten, so no confirm step is needed.\n\nSessions: reversible replace mode uses a session; a restore needs the same session id, plus a durable store (server started with --session-dir and --key-file) to survive a restart.\n\nHit a bug or a gap? Use send_feedback: it sanitizes your text locally and returns a prefilled GitHub issue URL you open and submit yourself. It sends nothing over the network.`;\n\nexport const createAnonymizeMcpServer = (\n service: LocalAnonymizeService,\n): McpServer => {\n // Synchronous by contract (public factory). The server version comes from the\n // package manifest, not a native call, so construction touches no binding; the\n // wasm binding is preloaded lazily on the first operation (see `#runOperation`).\n const server = new McpServer(\n {\n name: \"stella-anonymize-local\",\n version: pkg.version,\n },\n {\n instructions: MCP_INSTRUCTIONS,\n },\n );\n server.registerTool(\n \"capabilities\",\n {\n description:\n \"Return the public runtime capability manifest and MCP surface metadata.\",\n inputSchema: z.object({}),\n annotations: {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n },\n },\n async () => guard(() => capabilitiesResult(service)),\n );\n server.registerTool(\n \"anonymize_text_file\",\n {\n description: \"Anonymize a local UTF-8 text file into a new local file.\",\n inputSchema: textInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () => result(await service.anonymizeText(input))),\n );\n server.registerTool(\n \"restore_text_file\",\n {\n description: \"Restore a text file using the configured session store.\",\n inputSchema: restoreInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () => result(await service.restoreText(input))),\n );\n server.registerTool(\n \"anonymize_text_file_with_external_detections\",\n {\n description:\n \"Anonymize a local UTF-8 text file with a provider-neutral ExternalDetectionBatch v1 JSON sidecar into a new local file.\",\n inputSchema: externalDetectionTextInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () =>\n result(await service.anonymizeTextWithExternalDetections(input)),\n ),\n );\n server.registerTool(\n \"anonymize_docx_file\",\n {\n description:\n \"Structure-preservingly anonymize a local DOCX into a new local DOCX.\",\n inputSchema: docxInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () => result(await service.anonymizeDocx(input))),\n );\n server.registerTool(\n \"anonymize_pdf_file\",\n {\n description:\n \"Destructively raster-anonymize a local PDF into a fresh image-only PDF. Returns aggregate verification only; it does not claim perfect OCR or detector recall.\",\n inputSchema: pdfInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () => result(await service.anonymizePdf(input))),\n );\n server.registerTool(\n \"restore_docx_file\",\n {\n description: \"Restore a DOCX using the configured session store.\",\n inputSchema: docxRestoreInput,\n annotations: { destructiveHint: false, idempotentHint: false },\n },\n async (input) =>\n guard(async () => result(await service.restoreDocx(input))),\n );\n server.registerTool(\n \"inspect_docx_file\",\n {\n description:\n \"Return only aggregate DOCX coverage and block counts; never document text.\",\n inputSchema: z.object({\n inputPath: z.string().min(1).max(PATH_MAX_CHARACTERS),\n }),\n annotations: {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n },\n },\n async ({ inputPath }) =>\n guard(async () => result(await service.inspectDocx(inputPath))),\n );\n server.registerTool(\n \"send_feedback\",\n {\n description:\n \"File a bug, feature request, or docs issue with the stella-anonymize maintainers. Sanitizes the title and body locally (emails, ids, secrets, URLs, IPs are redacted) and returns a prefilled GitHub new-issue URL and a gh command that you open and submit under your own account. It sends nothing over the network and publishes nothing on its own. Never include document text, client names, ids, or secrets; describe the problem, steps, and expected vs actual result.\",\n inputSchema: feedbackInput,\n annotations: {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n },\n },\n async (input) => guard(() => feedbackResult(input)),\n );\n return server;\n};\n"],"mappings":";;;;;;;;;;;;;;;;ACcA,MAAa,4BAA4B;AACzC,MAAa,4BAA4B;AACzC,MAAa,4BAA4B;AACzC,MAAa,kCAAkC,MAAM,OAAO;AAE5D,MAAM,eAAe;AACrB,MAAMA,eAAa;AACnB,MAAM,eACJ;AACF,MAAMC,qBAAmB,KAAK;AAC9B,MAAM,iBAAiB;AAEvB,MAAa,+BAA+B;CAC1C,sBAAsB;CACtB,cAAc;CACd,oBAAoB;CACpB,oBAAoB;AACtB;AAmBA,MAAMC,cAAY,MAAoB,UACpC,KAAK,QAAQ,MAA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ACE;EACF,aAAa;EACb,aAAa;GAAE,iBAAiB;GAAO,gBAAgB;EAAM;CAC/D,GACA,OAAO,UACL,MAAM,YAAY,OAAO,MAAM,QAAQ,aAAa,KAAK,CAAC,CAAC,CAC/D;CACA,OAAO,aACL,qBACA;EACE,aAAa;EACb,aAAa;EACb,aAAa;GAAE,iBAAiB;GAAO,gBAAgB;EAAM;CAC/D,GACA,OAAO,UACL,MAAM,YAAY,OAAO,MAAM,QAAQ,YAAY,KAAK,CAAC,CAAC,CAC9D;CACA,OAAO,aACL,qBACA;EACE,aACE;EACF,aAAa,EAAE,OAAO,EACpB,WAAW,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,mBAAmB,EACtD,CAAC;EACD,aAAa;GACX,cAAc;GACd,iBAAiB;GACjB,gBAAgB;EAClB;CACF,GACA,OAAO,EAAE,gBACP,MAAM,YAAY,OAAO,MAAM,QAAQ,YAAY,SAAS,CAAC,CAAC,CAClE;CACA,OAAO,aACL,iBACA;EACE,aACE;EACF,aAAa;EACb,aAAa;GACX,cAAc;GACd,iBAAiB;GACjB,gBAAgB;GAChB,eAAe;EACjB;CACF,GACA,OAAO,UAAU,YAAY,eAAe,KAAK,CAAC,CACpD;CACA,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stll/anonymize-mcp",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "description": "Path-only local MCP server for stella PII anonymization",
5
5
  "type": "module",
6
6
  "bin": {
@@ -33,15 +33,15 @@
33
33
  "scripts": {
34
34
  "build": "tsdown",
35
35
  "typecheck": "bun ../../scripts/tsc-native.ts --noEmit -p tsconfig.json && bun ../../scripts/tsc-native.ts --noEmit -p tsconfig.test.json",
36
- "test": "bun test src/__test__/local.test.ts src/__test__/server.test.ts && bun run test:node-runtime",
36
+ "test": "bun test src/__test__/local.test.ts src/__test__/server.test.ts src/__test__/instructions.test.ts && bun run test:node-runtime",
37
37
  "test:node-runtime": "node --import tsx --test src/__test__/durable-sessions.test.ts && node scripts/test-packed-bin.mjs",
38
38
  "format": "oxfmt ."
39
39
  },
40
40
  "dependencies": {
41
41
  "@modelcontextprotocol/sdk": "^1.29.0",
42
- "@stll/anonymize": "2.4.2",
43
- "@stll/anonymize-docx": "^2.4.2",
44
- "@stll/anonymize-pdf": "2.4.2",
42
+ "@stll/anonymize": "2.5.0",
43
+ "@stll/anonymize-docx": "^2.5.0",
44
+ "@stll/anonymize-pdf": "2.5.0",
45
45
  "fs-native-extensions": "^1.5.0",
46
46
  "zod": "^4.4.3"
47
47
  },