narrarium-server 0.1.1 → 0.1.2

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/server.mjs CHANGED
@@ -112,17 +112,17 @@ var require_visit = __commonJS({
112
112
  visit.BREAK = BREAK;
113
113
  visit.SKIP = SKIP;
114
114
  visit.REMOVE = REMOVE;
115
- function visit_(key, node, visitor, path10) {
116
- const ctrl = callVisitor(key, node, visitor, path10);
115
+ function visit_(key, node, visitor, path11) {
116
+ const ctrl = callVisitor(key, node, visitor, path11);
117
117
  if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
118
- replaceNode(key, path10, ctrl);
119
- return visit_(key, ctrl, visitor, path10);
118
+ replaceNode(key, path11, ctrl);
119
+ return visit_(key, ctrl, visitor, path11);
120
120
  }
121
121
  if (typeof ctrl !== "symbol") {
122
122
  if (identity.isCollection(node)) {
123
- path10 = Object.freeze(path10.concat(node));
123
+ path11 = Object.freeze(path11.concat(node));
124
124
  for (let i = 0; i < node.items.length; ++i) {
125
- const ci = visit_(i, node.items[i], visitor, path10);
125
+ const ci = visit_(i, node.items[i], visitor, path11);
126
126
  if (typeof ci === "number")
127
127
  i = ci - 1;
128
128
  else if (ci === BREAK)
@@ -133,13 +133,13 @@ var require_visit = __commonJS({
133
133
  }
134
134
  }
135
135
  } else if (identity.isPair(node)) {
136
- path10 = Object.freeze(path10.concat(node));
137
- const ck = visit_("key", node.key, visitor, path10);
136
+ path11 = Object.freeze(path11.concat(node));
137
+ const ck = visit_("key", node.key, visitor, path11);
138
138
  if (ck === BREAK)
139
139
  return BREAK;
140
140
  else if (ck === REMOVE)
141
141
  node.key = null;
142
- const cv = visit_("value", node.value, visitor, path10);
142
+ const cv = visit_("value", node.value, visitor, path11);
143
143
  if (cv === BREAK)
144
144
  return BREAK;
145
145
  else if (cv === REMOVE)
@@ -160,17 +160,17 @@ var require_visit = __commonJS({
160
160
  visitAsync.BREAK = BREAK;
161
161
  visitAsync.SKIP = SKIP;
162
162
  visitAsync.REMOVE = REMOVE;
163
- async function visitAsync_(key, node, visitor, path10) {
164
- const ctrl = await callVisitor(key, node, visitor, path10);
163
+ async function visitAsync_(key, node, visitor, path11) {
164
+ const ctrl = await callVisitor(key, node, visitor, path11);
165
165
  if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
166
- replaceNode(key, path10, ctrl);
167
- return visitAsync_(key, ctrl, visitor, path10);
166
+ replaceNode(key, path11, ctrl);
167
+ return visitAsync_(key, ctrl, visitor, path11);
168
168
  }
169
169
  if (typeof ctrl !== "symbol") {
170
170
  if (identity.isCollection(node)) {
171
- path10 = Object.freeze(path10.concat(node));
171
+ path11 = Object.freeze(path11.concat(node));
172
172
  for (let i = 0; i < node.items.length; ++i) {
173
- const ci = await visitAsync_(i, node.items[i], visitor, path10);
173
+ const ci = await visitAsync_(i, node.items[i], visitor, path11);
174
174
  if (typeof ci === "number")
175
175
  i = ci - 1;
176
176
  else if (ci === BREAK)
@@ -181,13 +181,13 @@ var require_visit = __commonJS({
181
181
  }
182
182
  }
183
183
  } else if (identity.isPair(node)) {
184
- path10 = Object.freeze(path10.concat(node));
185
- const ck = await visitAsync_("key", node.key, visitor, path10);
184
+ path11 = Object.freeze(path11.concat(node));
185
+ const ck = await visitAsync_("key", node.key, visitor, path11);
186
186
  if (ck === BREAK)
187
187
  return BREAK;
188
188
  else if (ck === REMOVE)
189
189
  node.key = null;
190
- const cv = await visitAsync_("value", node.value, visitor, path10);
190
+ const cv = await visitAsync_("value", node.value, visitor, path11);
191
191
  if (cv === BREAK)
192
192
  return BREAK;
193
193
  else if (cv === REMOVE)
@@ -214,23 +214,23 @@ var require_visit = __commonJS({
214
214
  }
215
215
  return visitor;
216
216
  }
217
- function callVisitor(key, node, visitor, path10) {
217
+ function callVisitor(key, node, visitor, path11) {
218
218
  if (typeof visitor === "function")
219
- return visitor(key, node, path10);
219
+ return visitor(key, node, path11);
220
220
  if (identity.isMap(node))
221
- return visitor.Map?.(key, node, path10);
221
+ return visitor.Map?.(key, node, path11);
222
222
  if (identity.isSeq(node))
223
- return visitor.Seq?.(key, node, path10);
223
+ return visitor.Seq?.(key, node, path11);
224
224
  if (identity.isPair(node))
225
- return visitor.Pair?.(key, node, path10);
225
+ return visitor.Pair?.(key, node, path11);
226
226
  if (identity.isScalar(node))
227
- return visitor.Scalar?.(key, node, path10);
227
+ return visitor.Scalar?.(key, node, path11);
228
228
  if (identity.isAlias(node))
229
- return visitor.Alias?.(key, node, path10);
229
+ return visitor.Alias?.(key, node, path11);
230
230
  return void 0;
231
231
  }
232
- function replaceNode(key, path10, node) {
233
- const parent = path10[path10.length - 1];
232
+ function replaceNode(key, path11, node) {
233
+ const parent = path11[path11.length - 1];
234
234
  if (identity.isCollection(parent)) {
235
235
  parent.items[key] = node;
236
236
  } else if (identity.isPair(parent)) {
@@ -842,10 +842,10 @@ var require_Collection = __commonJS({
842
842
  var createNode = require_createNode();
843
843
  var identity = require_identity();
844
844
  var Node = require_Node();
845
- function collectionFromPath(schema, path10, value) {
845
+ function collectionFromPath(schema, path11, value) {
846
846
  let v = value;
847
- for (let i = path10.length - 1; i >= 0; --i) {
848
- const k = path10[i];
847
+ for (let i = path11.length - 1; i >= 0; --i) {
848
+ const k = path11[i];
849
849
  if (typeof k === "number" && Number.isInteger(k) && k >= 0) {
850
850
  const a = [];
851
851
  a[k] = v;
@@ -864,7 +864,7 @@ var require_Collection = __commonJS({
864
864
  sourceObjects: /* @__PURE__ */ new Map()
865
865
  });
866
866
  }
867
- var isEmptyPath = (path10) => path10 == null || typeof path10 === "object" && !!path10[Symbol.iterator]().next().done;
867
+ var isEmptyPath = (path11) => path11 == null || typeof path11 === "object" && !!path11[Symbol.iterator]().next().done;
868
868
  var Collection = class extends Node.NodeBase {
869
869
  constructor(type, schema) {
870
870
  super(type);
@@ -894,11 +894,11 @@ var require_Collection = __commonJS({
894
894
  * be a Pair instance or a `{ key, value }` object, which may not have a key
895
895
  * that already exists in the map.
896
896
  */
897
- addIn(path10, value) {
898
- if (isEmptyPath(path10))
897
+ addIn(path11, value) {
898
+ if (isEmptyPath(path11))
899
899
  this.add(value);
900
900
  else {
901
- const [key, ...rest] = path10;
901
+ const [key, ...rest] = path11;
902
902
  const node = this.get(key, true);
903
903
  if (identity.isCollection(node))
904
904
  node.addIn(rest, value);
@@ -912,8 +912,8 @@ var require_Collection = __commonJS({
912
912
  * Removes a value from the collection.
913
913
  * @returns `true` if the item was found and removed.
914
914
  */
915
- deleteIn(path10) {
916
- const [key, ...rest] = path10;
915
+ deleteIn(path11) {
916
+ const [key, ...rest] = path11;
917
917
  if (rest.length === 0)
918
918
  return this.delete(key);
919
919
  const node = this.get(key, true);
@@ -927,8 +927,8 @@ var require_Collection = __commonJS({
927
927
  * scalar values from their surrounding node; to disable set `keepScalar` to
928
928
  * `true` (collections are always returned intact).
929
929
  */
930
- getIn(path10, keepScalar) {
931
- const [key, ...rest] = path10;
930
+ getIn(path11, keepScalar) {
931
+ const [key, ...rest] = path11;
932
932
  const node = this.get(key, true);
933
933
  if (rest.length === 0)
934
934
  return !keepScalar && identity.isScalar(node) ? node.value : node;
@@ -946,8 +946,8 @@ var require_Collection = __commonJS({
946
946
  /**
947
947
  * Checks if the collection includes a value with the key `key`.
948
948
  */
949
- hasIn(path10) {
950
- const [key, ...rest] = path10;
949
+ hasIn(path11) {
950
+ const [key, ...rest] = path11;
951
951
  if (rest.length === 0)
952
952
  return this.has(key);
953
953
  const node = this.get(key, true);
@@ -957,8 +957,8 @@ var require_Collection = __commonJS({
957
957
  * Sets a value in this collection. For `!!set`, `value` needs to be a
958
958
  * boolean to add/remove the item from the set.
959
959
  */
960
- setIn(path10, value) {
961
- const [key, ...rest] = path10;
960
+ setIn(path11, value) {
961
+ const [key, ...rest] = path11;
962
962
  if (rest.length === 0) {
963
963
  this.set(key, value);
964
964
  } else {
@@ -3473,9 +3473,9 @@ var require_Document = __commonJS({
3473
3473
  this.contents.add(value);
3474
3474
  }
3475
3475
  /** Adds a value to the document. */
3476
- addIn(path10, value) {
3476
+ addIn(path11, value) {
3477
3477
  if (assertCollection(this.contents))
3478
- this.contents.addIn(path10, value);
3478
+ this.contents.addIn(path11, value);
3479
3479
  }
3480
3480
  /**
3481
3481
  * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
@@ -3550,14 +3550,14 @@ var require_Document = __commonJS({
3550
3550
  * Removes a value from the document.
3551
3551
  * @returns `true` if the item was found and removed.
3552
3552
  */
3553
- deleteIn(path10) {
3554
- if (Collection.isEmptyPath(path10)) {
3553
+ deleteIn(path11) {
3554
+ if (Collection.isEmptyPath(path11)) {
3555
3555
  if (this.contents == null)
3556
3556
  return false;
3557
3557
  this.contents = null;
3558
3558
  return true;
3559
3559
  }
3560
- return assertCollection(this.contents) ? this.contents.deleteIn(path10) : false;
3560
+ return assertCollection(this.contents) ? this.contents.deleteIn(path11) : false;
3561
3561
  }
3562
3562
  /**
3563
3563
  * Returns item at `key`, or `undefined` if not found. By default unwraps
@@ -3572,10 +3572,10 @@ var require_Document = __commonJS({
3572
3572
  * scalar values from their surrounding node; to disable set `keepScalar` to
3573
3573
  * `true` (collections are always returned intact).
3574
3574
  */
3575
- getIn(path10, keepScalar) {
3576
- if (Collection.isEmptyPath(path10))
3575
+ getIn(path11, keepScalar) {
3576
+ if (Collection.isEmptyPath(path11))
3577
3577
  return !keepScalar && identity.isScalar(this.contents) ? this.contents.value : this.contents;
3578
- return identity.isCollection(this.contents) ? this.contents.getIn(path10, keepScalar) : void 0;
3578
+ return identity.isCollection(this.contents) ? this.contents.getIn(path11, keepScalar) : void 0;
3579
3579
  }
3580
3580
  /**
3581
3581
  * Checks if the document includes a value with the key `key`.
@@ -3586,10 +3586,10 @@ var require_Document = __commonJS({
3586
3586
  /**
3587
3587
  * Checks if the document includes a value at `path`.
3588
3588
  */
3589
- hasIn(path10) {
3590
- if (Collection.isEmptyPath(path10))
3589
+ hasIn(path11) {
3590
+ if (Collection.isEmptyPath(path11))
3591
3591
  return this.contents !== void 0;
3592
- return identity.isCollection(this.contents) ? this.contents.hasIn(path10) : false;
3592
+ return identity.isCollection(this.contents) ? this.contents.hasIn(path11) : false;
3593
3593
  }
3594
3594
  /**
3595
3595
  * Sets a value in this document. For `!!set`, `value` needs to be a
@@ -3606,13 +3606,13 @@ var require_Document = __commonJS({
3606
3606
  * Sets a value in this document. For `!!set`, `value` needs to be a
3607
3607
  * boolean to add/remove the item from the set.
3608
3608
  */
3609
- setIn(path10, value) {
3610
- if (Collection.isEmptyPath(path10)) {
3609
+ setIn(path11, value) {
3610
+ if (Collection.isEmptyPath(path11)) {
3611
3611
  this.contents = value;
3612
3612
  } else if (this.contents == null) {
3613
- this.contents = Collection.collectionFromPath(this.schema, Array.from(path10), value);
3613
+ this.contents = Collection.collectionFromPath(this.schema, Array.from(path11), value);
3614
3614
  } else if (assertCollection(this.contents)) {
3615
- this.contents.setIn(path10, value);
3615
+ this.contents.setIn(path11, value);
3616
3616
  }
3617
3617
  }
3618
3618
  /**
@@ -5573,9 +5573,9 @@ var require_cst_visit = __commonJS({
5573
5573
  visit.BREAK = BREAK;
5574
5574
  visit.SKIP = SKIP;
5575
5575
  visit.REMOVE = REMOVE;
5576
- visit.itemAtPath = (cst, path10) => {
5576
+ visit.itemAtPath = (cst, path11) => {
5577
5577
  let item = cst;
5578
- for (const [field, index] of path10) {
5578
+ for (const [field, index] of path11) {
5579
5579
  const tok = item?.[field];
5580
5580
  if (tok && "items" in tok) {
5581
5581
  item = tok.items[index];
@@ -5584,23 +5584,23 @@ var require_cst_visit = __commonJS({
5584
5584
  }
5585
5585
  return item;
5586
5586
  };
5587
- visit.parentCollection = (cst, path10) => {
5588
- const parent = visit.itemAtPath(cst, path10.slice(0, -1));
5589
- const field = path10[path10.length - 1][0];
5587
+ visit.parentCollection = (cst, path11) => {
5588
+ const parent = visit.itemAtPath(cst, path11.slice(0, -1));
5589
+ const field = path11[path11.length - 1][0];
5590
5590
  const coll = parent?.[field];
5591
5591
  if (coll && "items" in coll)
5592
5592
  return coll;
5593
5593
  throw new Error("Parent collection not found");
5594
5594
  };
5595
- function _visit(path10, item, visitor) {
5596
- let ctrl = visitor(item, path10);
5595
+ function _visit(path11, item, visitor) {
5596
+ let ctrl = visitor(item, path11);
5597
5597
  if (typeof ctrl === "symbol")
5598
5598
  return ctrl;
5599
5599
  for (const field of ["key", "value"]) {
5600
5600
  const token = item[field];
5601
5601
  if (token && "items" in token) {
5602
5602
  for (let i = 0; i < token.items.length; ++i) {
5603
- const ci = _visit(Object.freeze(path10.concat([[field, i]])), token.items[i], visitor);
5603
+ const ci = _visit(Object.freeze(path11.concat([[field, i]])), token.items[i], visitor);
5604
5604
  if (typeof ci === "number")
5605
5605
  i = ci - 1;
5606
5606
  else if (ci === BREAK)
@@ -5611,10 +5611,10 @@ var require_cst_visit = __commonJS({
5611
5611
  }
5612
5612
  }
5613
5613
  if (typeof ctrl === "function" && field === "key")
5614
- ctrl = ctrl(item, path10);
5614
+ ctrl = ctrl(item, path11);
5615
5615
  }
5616
5616
  }
5617
- return typeof ctrl === "function" ? ctrl(item, path10) : ctrl;
5617
+ return typeof ctrl === "function" ? ctrl(item, path11) : ctrl;
5618
5618
  }
5619
5619
  exports.visit = visit;
5620
5620
  }
@@ -7366,7 +7366,7 @@ var require_dist = __commonJS({
7366
7366
  });
7367
7367
 
7368
7368
  // src/main.ts
7369
- import path9 from "node:path";
7369
+ import path10 from "node:path";
7370
7370
 
7371
7371
  // src/network.ts
7372
7372
  import { lookup } from "node:dns/promises";
@@ -7459,12 +7459,12 @@ function installOutboundNetwork(approvedBases) {
7459
7459
  import { spawn } from "node:child_process";
7460
7460
  import { constants, existsSync } from "node:fs";
7461
7461
  import * as fs from "node:fs/promises";
7462
- import { createHash, randomUUID } from "node:crypto";
7462
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
7463
7463
  import { homedir } from "node:os";
7464
7464
  import path from "node:path";
7465
7465
 
7466
7466
  // ../engine/dist/version.js
7467
- var ENGINE_VERSION = "0.1.1";
7467
+ var ENGINE_VERSION = "0.1.2";
7468
7468
 
7469
7469
  // ../engine/dist/util/hash.js
7470
7470
  var encoder = new TextEncoder();
@@ -7573,8 +7573,8 @@ var StructuredReplyError = class extends Error {
7573
7573
  }
7574
7574
  };
7575
7575
  function matchingBracket(text3, start) {
7576
- const open5 = text3[start];
7577
- const close = open5 === "{" ? "}" : "]";
7576
+ const open6 = text3[start];
7577
+ const close = open6 === "{" ? "}" : "]";
7578
7578
  let depth = 0;
7579
7579
  let inString = false;
7580
7580
  let escaped = false;
@@ -7591,7 +7591,7 @@ function matchingBracket(text3, start) {
7591
7591
  }
7592
7592
  if (character === '"')
7593
7593
  inString = true;
7594
- else if (character === open5)
7594
+ else if (character === open6)
7595
7595
  depth += 1;
7596
7596
  else if (character === close) {
7597
7597
  depth -= 1;
@@ -7857,12 +7857,12 @@ function canonPath(kind, slug) {
7857
7857
  }
7858
7858
  var CHAPTER_FILE_PATTERN = /^chapters\/([^/]+)\/chapter\.md$/;
7859
7859
  var PARAGRAPH_FILE_PATTERN = /^chapters\/([^/]+)\/(\d{3}(?:-[^/]+)?)\.md$/;
7860
- function parseChapterFilePath(path10) {
7861
- const match = CHAPTER_FILE_PATTERN.exec(path10);
7860
+ function parseChapterFilePath(path11) {
7861
+ const match = CHAPTER_FILE_PATTERN.exec(path11);
7862
7862
  return match ? { chapter: match[1] } : null;
7863
7863
  }
7864
- function parseParagraphFilePath(path10) {
7865
- const match = PARAGRAPH_FILE_PATTERN.exec(path10);
7864
+ function parseParagraphFilePath(path11) {
7865
+ const match = PARAGRAPH_FILE_PATTERN.exec(path11);
7866
7866
  return match ? { chapter: match[1], paragraph: match[2], number: Number(match[2].slice(0, 3)) } : null;
7867
7867
  }
7868
7868
  function chapterNumberFromSlug(slug) {
@@ -7882,25 +7882,51 @@ function directorOpinionPath(target2) {
7882
7882
  return `evaluations/director/chapters/${target2.chapter}.md`;
7883
7883
  return `evaluations/director/paragraphs/${target2.chapter}/${target2.paragraph}.md`;
7884
7884
  }
7885
- function isTextBookPath(path10) {
7886
- return /\.(md|markdown|txt|json|ya?ml)$/i.test(path10);
7887
- }
7888
- function assertSafeBookPath(path10) {
7889
- const normalized = path10.replace(/\\/g, "/").trim();
7890
- if (!normalized || normalized.startsWith("/") || /^[a-zA-Z]:/.test(normalized))
7891
- throw new Error(`Unsafe book path: ${path10}`);
7892
- const segments = normalized.split("/");
7893
- if (segments.some((segment) => !segment || segment === "." || segment === ".." || segment === ".git"))
7894
- throw new Error(`Unsafe book path: ${path10}`);
7885
+ function isTextBookPath(path11) {
7886
+ return /\.(md|markdown|txt|json|ya?ml)$/i.test(path11);
7887
+ }
7888
+ var MAX_PATH_LENGTH = 1024;
7889
+ var MAX_SEGMENT_LENGTH = 255;
7890
+ var IGNORED_BY_FILE_SYSTEMS = /[\u00ad\u200b-\u200f\u202a-\u202e\u2060-\u206f\ufeff]/gu;
7891
+ var FORBIDDEN_CHARACTERS = /[\u0000-\u001f\u007f-\u009f"*:<>?|]/u;
7892
+ var WINDOWS_DEVICE_NAME = /^(?:con|prn|aux|nul|com[0-9\u00b9\u00b2\u00b3]|lpt[0-9\u00b9\u00b2\u00b3])(?:\..*)?$/i;
7893
+ function isRepositoryMetadataName(segment) {
7894
+ const name = segment.normalize("NFKC").replace(IGNORED_BY_FILE_SYSTEMS, "").replace(/[. ]+$/, "").toLowerCase();
7895
+ return name === ".git" || name === "git~1";
7896
+ }
7897
+ function assertSafeBookPath(path11) {
7898
+ const unsafe = () => {
7899
+ throw new Error(`Unsafe book path: ${path11}`);
7900
+ };
7901
+ if (typeof path11 !== "string" || !path11 || path11.length > MAX_PATH_LENGTH)
7902
+ return unsafe();
7903
+ const normalized = path11.replace(/\\/g, "/");
7904
+ if (normalized.startsWith("/") || /^[a-zA-Z]:/.test(normalized))
7905
+ return unsafe();
7906
+ for (const segment of normalized.split("/")) {
7907
+ if (!segment || segment.length > MAX_SEGMENT_LENGTH || segment === "." || segment === "..")
7908
+ return unsafe();
7909
+ if (segment !== segment.trim() || /[. ]$/.test(segment) || FORBIDDEN_CHARACTERS.test(segment))
7910
+ return unsafe();
7911
+ if (isRepositoryMetadataName(segment) || WINDOWS_DEVICE_NAME.test(segment))
7912
+ return unsafe();
7913
+ }
7895
7914
  return normalized;
7896
7915
  }
7916
+ function isSafeBookPath(path11) {
7917
+ try {
7918
+ return !path11.includes("\\") && assertSafeBookPath(path11) === path11;
7919
+ } catch {
7920
+ return false;
7921
+ }
7922
+ }
7897
7923
 
7898
7924
  // ../engine/dist/book/store.js
7899
7925
  var BookConflictError = class extends Error {
7900
7926
  path;
7901
- constructor(message, path10) {
7927
+ constructor(message, path11) {
7902
7928
  super(message);
7903
- this.path = path10;
7929
+ this.path = path11;
7904
7930
  this.name = "BookConflictError";
7905
7931
  }
7906
7932
  };
@@ -7919,25 +7945,25 @@ function normalizeChanges(changes) {
7919
7945
  // ../engine/dist/book/snapshot.js
7920
7946
  var SnapshotCache = class {
7921
7947
  entries = /* @__PURE__ */ new Map();
7922
- async header(store, path10, hash2) {
7923
- const cached = this.entries.get(path10);
7948
+ async header(store, path11, hash2) {
7949
+ const cached = this.entries.get(path11);
7924
7950
  if (cached && cached.hash === hash2)
7925
7951
  return cached;
7926
- const file = await store.read(path10);
7952
+ const file = await store.read(path11);
7927
7953
  if (!file)
7928
7954
  return null;
7929
7955
  const document = parseMarkdown(file.content);
7930
7956
  const entry = { hash: file.hash, frontmatter: document.frontmatter, body: document.body.slice(0, 600) };
7931
- this.entries.set(path10, entry);
7957
+ this.entries.set(path11, entry);
7932
7958
  return entry;
7933
7959
  }
7934
7960
  };
7935
7961
  var CANON_PREFIXES = Object.entries(CANON_DIRECTORIES);
7936
- function canonKindOf(path10) {
7937
- if (!path10.endsWith(".md"))
7962
+ function canonKindOf(path11) {
7963
+ if (!path11.endsWith(".md"))
7938
7964
  return null;
7939
7965
  for (const [kind, directory] of CANON_PREFIXES) {
7940
- if (path10.startsWith(`${directory}/`) && !path10.slice(directory.length + 1).includes("/"))
7966
+ if (path11.startsWith(`${directory}/`) && !path11.slice(directory.length + 1).includes("/"))
7941
7967
  return kind;
7942
7968
  }
7943
7969
  return null;
@@ -7971,10 +7997,10 @@ async function loadBookSnapshot(store, cache = new SnapshotCache()) {
7971
7997
  const canon = [];
7972
7998
  const personaPaths = [];
7973
7999
  const ghostwriterSlugs = [];
7974
- await Promise.all(entries.map(async ({ path: path10, hash: hash2 }) => {
7975
- const chapterFile = parseChapterFilePath(path10);
8000
+ await Promise.all(entries.map(async ({ path: path11, hash: hash2 }) => {
8001
+ const chapterFile = parseChapterFilePath(path11);
7976
8002
  if (chapterFile) {
7977
- const header2 = await cache.header(store, path10, hash2);
8003
+ const header2 = await cache.header(store, path11, hash2);
7978
8004
  const chapter = ensureChapter(chapterFile.chapter);
7979
8005
  const frontmatter = header2?.frontmatter ?? {};
7980
8006
  chapter.hash = hash2;
@@ -7985,9 +8011,9 @@ async function loadBookSnapshot(store, cache = new SnapshotCache()) {
7985
8011
  chapter.ghostwriter = frontmatterString(frontmatter, "ghostwriter");
7986
8012
  return;
7987
8013
  }
7988
- const paragraphFile = parseParagraphFilePath(path10);
8014
+ const paragraphFile = parseParagraphFilePath(path11);
7989
8015
  if (paragraphFile) {
7990
- const header2 = await cache.header(store, path10, hash2);
8016
+ const header2 = await cache.header(store, path11, hash2);
7991
8017
  const frontmatter = header2?.frontmatter ?? {};
7992
8018
  ensureChapter(paragraphFile.chapter).paragraphs.push({
7993
8019
  chapter: paragraphFile.chapter,
@@ -7997,16 +8023,16 @@ async function loadBookSnapshot(store, cache = new SnapshotCache()) {
7997
8023
  summary: frontmatterString(frontmatter, "summary"),
7998
8024
  viewpoint: frontmatterString(frontmatter, "viewpoint"),
7999
8025
  ghostwriter: frontmatterString(frontmatter, "ghostwriter"),
8000
- path: path10,
8026
+ path: path11,
8001
8027
  hash: hash2
8002
8028
  });
8003
8029
  return;
8004
8030
  }
8005
- const kind = canonKindOf(path10);
8031
+ const kind = canonKindOf(path11);
8006
8032
  if (kind) {
8007
- const header2 = await cache.header(store, path10, hash2);
8033
+ const header2 = await cache.header(store, path11, hash2);
8008
8034
  const frontmatter = header2?.frontmatter ?? {};
8009
- const slug = path10.split("/").pop().replace(/\.md$/i, "");
8035
+ const slug = path11.split("/").pop().replace(/\.md$/i, "");
8010
8036
  canon.push({
8011
8037
  kind,
8012
8038
  slug,
@@ -8015,15 +8041,15 @@ async function loadBookSnapshot(store, cache = new SnapshotCache()) {
8015
8041
  aliases: frontmatterList(frontmatter, "aliases"),
8016
8042
  knownFrom: frontmatterString(frontmatter, "known_from"),
8017
8043
  revealIn: frontmatterString(frontmatter, "reveal_in"),
8018
- path: path10,
8044
+ path: path11,
8019
8045
  hash: hash2
8020
8046
  });
8021
8047
  return;
8022
8048
  }
8023
- if (/^personas\/[^/]+\.md$/.test(path10))
8024
- personaPaths.push(path10);
8025
- else if (/^ghostwriters\/[^/]+\.md$/.test(path10))
8026
- ghostwriterSlugs.push(path10.slice("ghostwriters/".length, -3));
8049
+ if (/^personas\/[^/]+\.md$/.test(path11))
8050
+ personaPaths.push(path11);
8051
+ else if (/^ghostwriters\/[^/]+\.md$/.test(path11))
8052
+ ghostwriterSlugs.push(path11.slice("ghostwriters/".length, -3));
8027
8053
  }));
8028
8054
  const orderedChapters = [...chapters.values()].map((chapter) => ({ ...chapter, paragraphs: chapter.paragraphs.sort((left, right) => left.number - right.number || left.slug.localeCompare(right.slug)) })).sort((left, right) => left.number - right.number || left.slug.localeCompare(right.slug));
8029
8055
  return {
@@ -8390,12 +8416,12 @@ function readerPersonaSystemPrompt(profile, outputLanguage, depth) {
8390
8416
  }
8391
8417
 
8392
8418
  // ../engine/dist/book/context.js
8393
- async function readDocument(store, path10) {
8394
- const file = await store.read(path10);
8419
+ async function readDocument(store, path11) {
8420
+ const file = await store.read(path11);
8395
8421
  return file ? { file, document: parseMarkdown(file.content) } : null;
8396
8422
  }
8397
- async function readBody(store, path10, max = 6e3, keep = "start") {
8398
- const loaded = await readDocument(store, path10);
8423
+ async function readBody(store, path11, max = 6e3, keep = "start") {
8424
+ const loaded = await readDocument(store, path11);
8399
8425
  if (!loaded)
8400
8426
  return "";
8401
8427
  return keep === "end" ? tail(loaded.document.body, max) : truncate(loaded.document.body, max);
@@ -8419,9 +8445,9 @@ function ghostwriterBlock(profile) {
8419
8445
  return profile ? ghostwriterPrompt(profile) : "No ghostwriter profile: write clear, concrete, professional narrative prose.";
8420
8446
  }
8421
8447
  async function loadReaderPanel(store, snapshot) {
8422
- const overrides = await Promise.all(snapshot.personaPaths.map(async (path10) => {
8423
- const file = await store.read(path10);
8424
- return file ? parseReaderPersona(path10.slice("personas/".length, -3), file.content) : null;
8448
+ const overrides = await Promise.all(snapshot.personaPaths.map(async (path11) => {
8449
+ const file = await store.read(path11);
8450
+ return file ? parseReaderPersona(path11.slice("personas/".length, -3), file.content) : null;
8425
8451
  }));
8426
8452
  return mergeReaderPersonas(snapshot.book.language, overrides.filter((profile) => Boolean(profile)));
8427
8453
  }
@@ -9182,24 +9208,59 @@ async function readEventStream(response, onData) {
9182
9208
  onData(data2);
9183
9209
  return false;
9184
9210
  };
9185
- for (; ; ) {
9186
- const { value, done } = await reader.read();
9187
- if (done)
9188
- break;
9189
- buffer += decoder.decode(value, { stream: true });
9190
- let newline = buffer.indexOf("\n");
9191
- while (newline >= 0) {
9192
- const line = buffer.slice(0, newline);
9193
- buffer = buffer.slice(newline + 1);
9194
- if (flushLine(line)) {
9195
- await reader.cancel().catch(() => void 0);
9196
- return true;
9211
+ try {
9212
+ for (; ; ) {
9213
+ const { value, done } = await reader.read();
9214
+ if (done)
9215
+ break;
9216
+ buffer += decoder.decode(value, { stream: true });
9217
+ let newline = buffer.indexOf("\n");
9218
+ while (newline >= 0) {
9219
+ const line = buffer.slice(0, newline);
9220
+ buffer = buffer.slice(newline + 1);
9221
+ if (flushLine(line)) {
9222
+ await reader.cancel().catch(() => void 0);
9223
+ return true;
9224
+ }
9225
+ newline = buffer.indexOf("\n");
9197
9226
  }
9198
- newline = buffer.indexOf("\n");
9199
9227
  }
9228
+ buffer += decoder.decode();
9229
+ return buffer.trim() ? flushLine(buffer) : false;
9230
+ } catch (error) {
9231
+ await reader.cancel().catch(() => void 0);
9232
+ throw error;
9200
9233
  }
9201
- buffer += decoder.decode();
9202
- return buffer.trim() ? flushLine(buffer) : false;
9234
+ }
9235
+ var COMPLETE_REASONS = /* @__PURE__ */ new Set(["stop", "tool_calls", "function_call", "end_turn", "stop_sequence", "tool_use", "eos"]);
9236
+ var TRUNCATED_REASONS = /* @__PURE__ */ new Set(["length", "max_tokens", "max_output_tokens", "model_length", "context_length_exceeded"]);
9237
+ var FILTERED_REASONS = /* @__PURE__ */ new Set(["content_filter", "safety", "blocked", "recitation", "prohibited_content"]);
9238
+ var FAILED_REASONS = /* @__PURE__ */ new Set(["error", "failed", "abort", "aborted", "cancelled", "canceled"]);
9239
+ function classifyFinishReason(reason, ended) {
9240
+ const key = reason.trim().toLowerCase().replace(/[\s-]+/g, "_");
9241
+ if (!key)
9242
+ return ended ? "complete" : "incomplete";
9243
+ if (COMPLETE_REASONS.has(key))
9244
+ return "complete";
9245
+ if (TRUNCATED_REASONS.has(key))
9246
+ return "truncated";
9247
+ if (FILTERED_REASONS.has(key))
9248
+ return "filtered";
9249
+ if (FAILED_REASONS.has(key))
9250
+ return "failed";
9251
+ return ended ? "complete" : "unexpected";
9252
+ }
9253
+ var PERMANENT_STREAM_ERROR = /authenticat|unauthori[sz]ed|forbidden|permission|invalid[_ -]?(?:request|api[_ -]?key|argument|parameter)|bad[_ -]?request/i;
9254
+ function streamError(value, label, details) {
9255
+ const source = value && typeof value === "object" ? value : null;
9256
+ const message = typeof value === "string" ? value.trim() : typeof source?.message === "string" ? source.message.trim() : "";
9257
+ if (!source && !message)
9258
+ return null;
9259
+ const status = [source?.status, source?.status_code, source?.code].map((entry) => typeof entry === "number" ? entry : typeof entry === "string" && /^\d{3}$/.test(entry.trim()) ? Number(entry) : Number.NaN).find((entry) => Number.isInteger(entry) && entry >= 100 && entry < 600);
9260
+ const words = [source?.type, source?.code, source?.status].filter((entry) => typeof entry === "string").join(" ");
9261
+ const permanent = status !== void 0 && status >= 400 && status < 500 && status !== 408 && status !== 409 && status !== 429 || PERMANENT_STREAM_ERROR.test(words);
9262
+ const detail = (message || (source && Object.keys(source).length ? JSON.stringify(source) : "no details")).replace(/\s+/g, " ").slice(0, 400);
9263
+ return new AiProviderError(`${label}: the provider reported an error during the reply \u2014 ${detail}`, status, !permanent, details);
9203
9264
  }
9204
9265
  function reasoningLength(source) {
9205
9266
  if (!source || typeof source !== "object")
@@ -9261,7 +9322,7 @@ ${jsonInstruction(request2.json)}` : request2.system;
9261
9322
  let usage;
9262
9323
  let finishReason = "";
9263
9324
  let reasoningCharacters = 0;
9264
- let complete = true;
9325
+ let ended = true;
9265
9326
  const details = (extra = {}) => ({
9266
9327
  usage,
9267
9328
  cost: estimateCost(usage, candidate2.pricing),
@@ -9273,32 +9334,38 @@ ${jsonInstruction(request2.json)}` : request2.system;
9273
9334
  });
9274
9335
  try {
9275
9336
  if (stream) {
9276
- const sawDone = await readEventStream(response, (data2) => {
9337
+ ended = await readEventStream(response, (data2) => {
9277
9338
  let chunk;
9278
9339
  try {
9279
9340
  chunk = JSON.parse(data2);
9280
9341
  } catch {
9281
9342
  return;
9282
9343
  }
9344
+ if (!chunk || typeof chunk !== "object")
9345
+ return;
9346
+ if (chunk.error) {
9347
+ const failure = streamError(chunk.error, label, details());
9348
+ if (failure)
9349
+ throw failure;
9350
+ }
9283
9351
  const delta = chunk.choices?.[0]?.delta;
9284
9352
  reasoningCharacters += reasoningLength(delta);
9285
9353
  if (delta?.content) {
9286
9354
  text3 += delta.content;
9287
9355
  request2.onText?.(text3);
9288
9356
  }
9289
- const reason = chunk.choices?.find((choice) => choice.finish_reason)?.finish_reason;
9357
+ const reason = chunk.choices?.find((choice) => typeof choice?.finish_reason === "string" && choice.finish_reason)?.finish_reason;
9290
9358
  if (reason)
9291
9359
  finishReason = reason;
9292
9360
  if (chunk.usage)
9293
9361
  usage = normalizeUsage(chunk.usage);
9294
9362
  });
9295
- complete = sawDone || Boolean(finishReason);
9296
9363
  } else {
9297
9364
  const payload = await response.json();
9298
9365
  const choice = payload.choices?.[0];
9299
9366
  text3 = typeof choice?.message?.content === "string" ? choice.message.content : "";
9300
9367
  toolArguments = choice?.message?.tool_calls?.find((call) => typeof call.function?.arguments === "string")?.function?.arguments ?? "";
9301
- finishReason = choice?.finish_reason ?? "";
9368
+ finishReason = typeof choice?.finish_reason === "string" ? choice.finish_reason : "";
9302
9369
  reasoningCharacters = reasoningLength(choice?.message);
9303
9370
  usage = normalizeUsage(payload.usage);
9304
9371
  }
@@ -9307,15 +9374,21 @@ ${jsonInstruction(request2.json)}` : request2.system;
9307
9374
  throw error;
9308
9375
  throw new AiProviderError(`${label}: ${timer.timedOut() ? "request timed out" : "the reply could not be read"}.`, void 0, true, details());
9309
9376
  }
9310
- if (!complete)
9377
+ const outcome = classifyFinishReason(finishReason, ended);
9378
+ const shown = finishReason.replace(/\s+/g, " ").trim().slice(0, 60);
9379
+ if (outcome === "incomplete")
9311
9380
  throw new AiProviderError(`${label}: the stream ended before the reply was complete.`, void 0, true, details());
9312
- if (finishReason === "content_filter")
9313
- throw new AiProviderError(`${label}: the provider filtered the reply content.`, void 0, false, details());
9381
+ if (outcome === "unexpected")
9382
+ throw new AiProviderError(`${label}: unexpected finish reason "${shown}" and the stream did not end with [DONE]: the reply may be incomplete.`, void 0, true, details());
9383
+ if (outcome === "failed")
9384
+ throw new AiProviderError(`${label}: the provider ended the reply with the finish reason "${shown}".`, void 0, true, details());
9385
+ if (outcome === "filtered")
9386
+ throw new AiProviderError(`${label}: the provider filtered the reply content (${shown}).`, void 0, false, details());
9314
9387
  const inline = splitReasoningBlock(text3);
9315
9388
  text3 = inline.reply;
9316
9389
  reasoningCharacters += inline.reasoningCharacters;
9317
9390
  const answered = Boolean(text3.trim() || toolArguments.trim());
9318
- if (finishReason === "length") {
9391
+ if (outcome === "truncated") {
9319
9392
  throw new AiProviderError(`${label}: ${!answered && reasoningCharacters > 0 ? "the model used its output limit while reasoning, before writing the reply" : "the reply was cut by the output token limit"}. Nothing was applied.`, void 0, false, details({ truncated: true }));
9320
9393
  }
9321
9394
  if (!answered)
@@ -10013,6 +10086,24 @@ function parseAutopilotState(raw, now = /* @__PURE__ */ new Date()) {
10013
10086
  return null;
10014
10087
  }
10015
10088
  }
10089
+ function inspectAutopilotState(raw, now = /* @__PURE__ */ new Date()) {
10090
+ if (!raw?.trim())
10091
+ return { state: null };
10092
+ let value;
10093
+ try {
10094
+ value = JSON.parse(raw);
10095
+ } catch {
10096
+ const conflicted = /^<{7}(?: |$)/m.test(raw) && /^>{7}(?: |$)/m.test(raw);
10097
+ return { state: null, unreadable: conflicted ? "it contains unresolved merge conflict markers" : "it is not valid JSON" };
10098
+ }
10099
+ if (!value || typeof value !== "object" || Array.isArray(value))
10100
+ return { state: null, unreadable: "it does not hold a JSON object" };
10101
+ try {
10102
+ return { state: normalizeAutopilotState(value, now) };
10103
+ } catch {
10104
+ return { state: null, unreadable: "its content could not be interpreted" };
10105
+ }
10106
+ }
10016
10107
  function serializeAutopilotState(state) {
10017
10108
  return `${JSON.stringify(state, null, 2)}
10018
10109
  `;
@@ -10338,7 +10429,7 @@ ${chapterLines.join("\n")}` : "CHAPTERS: none yet.",
10338
10429
  directorNotes ? `STANDING RULES AND NOTES:
10339
10430
  ${directorNotes}` : ""
10340
10431
  ],
10341
- sourceHash: await sha256Hex([...snapshot.files.entries()].filter(([path10]) => path10.startsWith("chapters/")).map(([path10, hash2]) => `${path10}:${hash2}`).sort().join("\n"))
10432
+ sourceHash: await sha256Hex([...snapshot.files.entries()].filter(([path11]) => path11.startsWith("chapters/")).map(([path11, hash2]) => `${path11}:${hash2}`).sort().join("\n"))
10342
10433
  };
10343
10434
  }
10344
10435
  async function chapterMaterial(store, snapshot, slug) {
@@ -10461,12 +10552,12 @@ ${untrustedData("repository_content", material.sections.filter(Boolean).join("\n
10461
10552
  currency: response.currency,
10462
10553
  sourceHash: material.sourceHash
10463
10554
  };
10464
- const path10 = directorOpinionPath(input.target);
10555
+ const path11 = directorOpinionPath(input.target);
10465
10556
  if (input.save) {
10466
- const existing = await input.store.read(path10);
10467
- await input.store.commit([{ path: path10, content: renderDirectorOpinion(opinion, language), expectedHash: existing?.hash ?? null }], `Director: opinion on ${material.title}`);
10557
+ const existing = await input.store.read(path11);
10558
+ await input.store.commit([{ path: path11, content: renderDirectorOpinion(opinion, language), expectedHash: existing?.hash ?? null }], `Director: opinion on ${material.title}`);
10468
10559
  }
10469
- return { opinion, path: path10, saved: Boolean(input.save) };
10560
+ return { opinion, path: path11, saved: Boolean(input.save) };
10470
10561
  }
10471
10562
 
10472
10563
  // ../engine/dist/autopilot/prompts.js
@@ -10907,15 +10998,15 @@ function assertProse(text3, minimumWords) {
10907
10998
  function normalizeForMatch(text3) {
10908
10999
  return text3.replace(/[«»“”„"]/g, '"').replace(/[‘’`]/g, "'").replace(/[–—]/g, "-").replace(/\s+/g, " ").trim().toLowerCase();
10909
11000
  }
10910
- async function changeFor(store, path10, render) {
10911
- const file = await store.read(path10);
11001
+ async function changeFor(store, path11, render) {
11002
+ const file = await store.read(path11);
10912
11003
  const existing = file ? parseMarkdown(file.content) : null;
10913
11004
  const content = render(existing ? { frontmatter: existing.frontmatter, body: existing.body } : null);
10914
11005
  if (content === null)
10915
11006
  return null;
10916
11007
  if (file && file.content === content)
10917
11008
  return null;
10918
- return { path: path10, content, expectedHash: file?.hash ?? null };
11009
+ return { path: path11, content, expectedHash: file?.hash ?? null };
10919
11010
  }
10920
11011
  function chapterDocument(chapter) {
10921
11012
  return renderMarkdown({ type: "chapter", id: `chapter:${chapter.slug}`, number: chapter.number, title: chapter.title, summary: chapter.summary, pov: chapter.pov, canon: "draft" }, `# ${chapter.title}
@@ -10923,8 +11014,8 @@ function chapterDocument(chapter) {
10923
11014
  ${chapter.goal || chapter.summary || ""}`);
10924
11015
  }
10925
11016
  function ensureChapterFile(ctx, chapter) {
10926
- const path10 = chapterFilePath(chapter.slug);
10927
- return ctx.snapshot.files.has(path10) ? null : { path: path10, content: chapterDocument(chapter), expectedHash: null };
11017
+ const path11 = chapterFilePath(chapter.slug);
11018
+ return ctx.snapshot.files.has(path11) ? null : { path: path11, content: chapterDocument(chapter), expectedHash: null };
10928
11019
  }
10929
11020
  function renderPlanMarkdown(state, language) {
10930
11021
  const italian = isItalian(language);
@@ -11069,17 +11160,17 @@ ${directorTaste(ctx.director)}`
11069
11160
  if (existing)
11070
11161
  return existing.slug;
11071
11162
  const slug = slugify(name);
11072
- const path10 = canonPath(kind, slug);
11073
- if (!slug || created.has(path10) || ctx.snapshot.files.has(path10))
11163
+ const path11 = canonPath(kind, slug);
11164
+ if (!slug || created.has(path11) || ctx.snapshot.files.has(path11))
11074
11165
  return slug || null;
11075
- created.add(path10);
11166
+ created.add(path11);
11076
11167
  known.push({ kind, slug, names: [name] });
11077
11168
  if (kind === "character")
11078
11169
  counts.character += 1;
11079
11170
  else
11080
11171
  counts.other += 1;
11081
11172
  const label = kind === "secret" ? { title: name } : { name };
11082
- changes.push({ path: path10, content: renderMarkdown({ type: kind, id: `${kind}:${slug}`, ...label, ...frontmatter, canon: "draft" }, body), expectedHash: null });
11173
+ changes.push({ path: path11, content: renderMarkdown({ type: kind, id: `${kind}:${slug}`, ...label, ...frontmatter, canon: "draft" }, body), expectedHash: null });
11083
11174
  return slug;
11084
11175
  };
11085
11176
  const characterSlug = (name) => known.find((entry) => entry.kind === "character" && sameCanonEntity("character", name, entry.names))?.slug ?? slugify(name);
@@ -11345,10 +11436,10 @@ ${plan.readerReactions.map((reaction) => `- ${reaction.name} (weight ${ctx.direc
11345
11436
  changes.push(ensureChapterFile(ctx, chapter));
11346
11437
  for (const secret2 of ctx.state.concept.secrets) {
11347
11438
  const chapter = plan.chapters.find((entry) => entry.number === secret2.revealChapter);
11348
- const path10 = canonPath("secret", secret2.slug);
11349
- if (!chapter || !ctx.snapshot.files.has(path10))
11439
+ const path11 = canonPath("secret", secret2.slug);
11440
+ if (!chapter || !ctx.snapshot.files.has(path11))
11350
11441
  continue;
11351
- changes.push(await changeFor(ctx.store, path10, (existing) => {
11442
+ changes.push(await changeFor(ctx.store, path11, (existing) => {
11352
11443
  if (!existing || frontmatterString(existing.frontmatter, "reveal_in"))
11353
11444
  return null;
11354
11445
  return renderMarkdown({ ...existing.frontmatter, reveal_in: `chapter:${chapter.slug}`, known_from: `chapter:${chapter.slug}` }, existing.body);
@@ -11519,18 +11610,18 @@ ${secrets}` : ""
11519
11610
  assertProse(prose, Math.min(150, Math.floor(words * 0.25)));
11520
11611
  const summary = await summarizeScene(ctx, chapter, scene, prose) ?? scene.summary ?? scene.goal;
11521
11612
  const slug = paragraphSlug(scene.number, scene.title);
11522
- const path10 = paragraphFilePath(chapter.slug, slug);
11613
+ const path11 = paragraphFilePath(chapter.slug, slug);
11523
11614
  const content = renderMarkdown({ type: "paragraph", id: paragraphId(chapter.slug, slug), chapter: `chapter:${chapter.slug}`, number: scene.number, title: scene.title, summary, viewpoint: scene.pov, canon: "draft" }, prose);
11524
11615
  scene.status = "written";
11525
11616
  scene.slug = slug;
11526
- scene.path = path10;
11617
+ scene.path = path11;
11527
11618
  scene.summary = summary;
11528
11619
  scene.words = countWords(prose);
11529
11620
  if (chapter.scenes.every((entry) => entry.status !== "planned"))
11530
11621
  chapter.status = "review";
11531
11622
  ctx.insight("ok", isItalian(ctx.language) ? `Scena ${chapter.number}.${scene.number} scritta: ${scene.words} parole` : `Scene ${chapter.number}.${scene.number} written: ${scene.words} words`, summary);
11532
11623
  return {
11533
- changes: compact([{ path: path10, content, expectedHash: ctx.snapshot.files.get(path10) ?? null }, ensureChapterFile(ctx, chapter), await planFileChange(ctx)]),
11624
+ changes: compact([{ path: path11, content, expectedHash: ctx.snapshot.files.get(path11) ?? null }, ensureChapterFile(ctx, chapter), await planFileChange(ctx)]),
11534
11625
  summary: `${scene.words} words \u2014 ${summary}`
11535
11626
  };
11536
11627
  }
@@ -11567,9 +11658,9 @@ ${resume}` : "",
11567
11658
  maxOutputTokens: 2500
11568
11659
  });
11569
11660
  const output = normalizeReaderEvaluationOutput(requireStructuredFields(response.json, { score: "number" }, "The reader evaluation"));
11570
- const path10 = readerEvaluationPath({ kind: "chapter", chapter: chapter.slug }, reader.slug);
11661
+ const path11 = readerEvaluationPath({ kind: "chapter", chapter: chapter.slug }, reader.slug);
11571
11662
  const updatedAt = ctx.now().toISOString();
11572
- const change = await changeFor(ctx.store, path10, (existing) => renderMarkdown({
11663
+ const change = await changeFor(ctx.store, path11, (existing) => renderMarkdown({
11573
11664
  ...existing?.frontmatter ?? {},
11574
11665
  id: `reader-evaluation:chapter:${chapter.slug}:chapter:${reader.slug}`,
11575
11666
  type: "readerEvaluation",
@@ -11601,7 +11692,7 @@ ${resume}` : "",
11601
11692
  status: "completed",
11602
11693
  generatedBy: "autopilot"
11603
11694
  }, renderReaderEvaluationBody(output, ctx.language)));
11604
- review.readerScores = [...review.readerScores.filter((entry) => entry.slug !== slug), { slug, name: reader.name, score: output.score, mainProblem: output.mainProblem, prioritySuggestion: output.prioritySuggestion, path: path10 }];
11695
+ review.readerScores = [...review.readerScores.filter((entry) => entry.slug !== slug), { slug, name: reader.name, score: output.score, mainProblem: output.mainProblem, prioritySuggestion: output.prioritySuggestion, path: path11 }];
11605
11696
  ctx.insight(output.score >= 7 ? "ok" : output.score >= 5 ? "info" : "warning", `${reader.name}: ${output.score}/10`, output.mainProblem || output.generalImpression);
11606
11697
  return { changes: compact([change]), summary: `${output.score}/10 \u2014 ${truncate(output.generalImpression, 220)}` };
11607
11698
  }
@@ -11624,9 +11715,9 @@ ${untrustedData("repository_content", evaluations.join("\n\n---\n\n"))}`,
11624
11715
  });
11625
11716
  const output = requireStructuredFields(response.json, { overallScore: "number" }, "The readers' summary");
11626
11717
  const overall = Math.round(asNumber(output.overallScore, 6, 1, 10) * 10) / 10;
11627
- const path10 = readerSummaryPath({ kind: "chapter", chapter: chapter.slug });
11718
+ const path11 = readerSummaryPath({ kind: "chapter", chapter: chapter.slug });
11628
11719
  const updatedAt = ctx.now().toISOString();
11629
- const change = await changeFor(ctx.store, path10, (existing) => renderMarkdown({
11720
+ const change = await changeFor(ctx.store, path11, (existing) => renderMarkdown({
11630
11721
  ...existing?.frontmatter ?? {},
11631
11722
  id: `reader-evaluation-summary:chapter:${chapter.slug}:chapter`,
11632
11723
  type: "readerEvaluationSummary",
@@ -12144,13 +12235,30 @@ var TASK_EXECUTORS = {
12144
12235
  // ../engine/dist/autopilot/runner.js
12145
12236
  async function readStoredAutopilot(store) {
12146
12237
  const [stateFile, activityFile] = await Promise.all([store.read(AUTOPILOT_STATE_FILE), store.read(AUTOPILOT_ACTIVITY_FILE)]);
12238
+ const reading = inspectAutopilotState(stateFile?.content);
12147
12239
  return {
12148
- state: parseAutopilotState(stateFile?.content),
12240
+ state: reading.state,
12149
12241
  stateHash: stateFile?.hash ?? null,
12150
12242
  activities: parseActivityLog(activityFile?.content),
12151
- activityHash: activityFile?.hash ?? null
12243
+ activityHash: activityFile?.hash ?? null,
12244
+ ...reading.unreadable ? { corrupt: { path: AUTOPILOT_STATE_FILE, reason: reading.unreadable } } : {}
12152
12245
  };
12153
12246
  }
12247
+ var AutopilotStateCorruptError = class extends Error {
12248
+ code = "state_corrupt";
12249
+ path;
12250
+ reason;
12251
+ constructor(problem) {
12252
+ super(`The autopilot state file ${problem.path} exists but cannot be read (${problem.reason}), so the autopilot leaves it untouched. Restore the file from the book's history, or reset the autopilot, which deletes the file so it can start over.`);
12253
+ this.name = "AutopilotStateCorruptError";
12254
+ this.path = problem.path;
12255
+ this.reason = problem.reason;
12256
+ }
12257
+ };
12258
+ function assertStoredStateReadable(stored) {
12259
+ if (stored.corrupt)
12260
+ throw new AutopilotStateCorruptError(stored.corrupt);
12261
+ }
12154
12262
  function stampState(state, now) {
12155
12263
  return { ...state, version: state.version + 1, updatedAt: now.toISOString() };
12156
12264
  }
@@ -12240,14 +12348,33 @@ function extendPlanIfNeeded(state) {
12240
12348
  function describeError(error) {
12241
12349
  return error instanceof Error ? error.message : String(error);
12242
12350
  }
12351
+ var storeFailures = /* @__PURE__ */ new WeakSet();
12243
12352
  function isRetryableError(error) {
12244
12353
  if (error instanceof StructuredReplyError)
12245
12354
  return true;
12246
12355
  if (!error || typeof error !== "object")
12247
12356
  return false;
12357
+ if (storeFailures.has(error))
12358
+ return true;
12248
12359
  const flags = error;
12249
12360
  return flags.retryable === true && flags.truncated !== true;
12250
12361
  }
12362
+ function transientStore(store) {
12363
+ const guard = async (operation) => {
12364
+ try {
12365
+ return await operation();
12366
+ } catch (error) {
12367
+ if (error && typeof error === "object" && !isBookConflictError(error) && !isAbortError(error) && error.retryable !== false)
12368
+ storeFailures.add(error);
12369
+ throw error;
12370
+ }
12371
+ };
12372
+ return {
12373
+ list: () => guard(() => store.list()),
12374
+ read: (path11) => guard(() => store.read(path11)),
12375
+ commit: (changes, message) => guard(() => store.commit(changes, message))
12376
+ };
12377
+ }
12251
12378
  var RETRY_NOTE_MAX = 500;
12252
12379
  function retryNoteFor(error) {
12253
12380
  if (!(error instanceof AiReplyRejectedError) && !(error instanceof StructuredReplyError))
@@ -12288,6 +12415,7 @@ function withSpend(stats, spend) {
12288
12415
  function taskKey(task) {
12289
12416
  return [task.kind, task.chapterNumber ?? "", task.sceneNumber ?? "", task.reader ?? ""].join(":");
12290
12417
  }
12418
+ var TAKEN_OVER = "Another runner took over this book: this run stops.";
12291
12419
  var AutopilotRun = class {
12292
12420
  config;
12293
12421
  runId = randomId("run");
@@ -12295,14 +12423,18 @@ var AutopilotRun = class {
12295
12423
  stepController = null;
12296
12424
  stopRequested = false;
12297
12425
  interrupted = false;
12426
+ /** Another run is recorded as the owner of the book: this one saves nothing more and ends. */
12427
+ leaseLost = false;
12298
12428
  queue = [];
12299
12429
  cache = new SnapshotCache();
12430
+ store;
12300
12431
  now;
12301
12432
  chaptersThisRun = 0;
12302
12433
  current = null;
12303
12434
  constructor(config) {
12304
12435
  this.config = config;
12305
12436
  this.now = config.now ?? (() => /* @__PURE__ */ new Date());
12437
+ this.store = transientStore(config.store);
12306
12438
  }
12307
12439
  get activity() {
12308
12440
  return this.current;
@@ -12335,9 +12467,9 @@ var AutopilotRun = class {
12335
12467
  async stepContext(snapshot, state) {
12336
12468
  const language = languageCode(snapshot.book.language);
12337
12469
  const [director, panel, design] = await Promise.all([
12338
- loadDirectorProfile(this.config.store, language),
12339
- loadReaderPanel(this.config.store, snapshot),
12340
- readBody(this.config.store, STORY_DESIGN_FILE, 2e4)
12470
+ loadDirectorProfile(this.store, language),
12471
+ loadReaderPanel(this.store, snapshot),
12472
+ readBody(this.store, STORY_DESIGN_FILE, 2e4)
12341
12473
  ]);
12342
12474
  const readers = activeReaders(panel, director, state.options.maxReaders).map((reader) => ({ slug: reader.slug, name: reader.name }));
12343
12475
  return { planner: { snapshot, director, readers, language, storyDesignThin: isThinDocument(design) }, director, panel };
@@ -12349,8 +12481,9 @@ var AutopilotRun = class {
12349
12481
  return next;
12350
12482
  }
12351
12483
  async execute(start = {}) {
12352
- const { store } = this.config;
12484
+ const { store } = this;
12353
12485
  const stored = await readStoredAutopilot(store);
12486
+ assertStoredStateReadable(stored);
12354
12487
  let state = stored.state ?? createAutopilotState(this.now(), start.brief, start.options);
12355
12488
  const startedAt = this.now().toISOString();
12356
12489
  state = extendPlanIfNeeded({
@@ -12367,6 +12500,17 @@ var AutopilotRun = class {
12367
12500
  await store.commit([stateChange(state, stored.stateHash)], "Autopilot: started");
12368
12501
  this.emit({ type: "run-started", runId: this.runId, host: this.config.host });
12369
12502
  this.emit({ type: "state", state: summarizeAutopilotState(state) });
12503
+ try {
12504
+ return await this.runSteps(state);
12505
+ } catch (error) {
12506
+ this.emit({ type: "run-ended", runId: this.runId, status: "error", message: describeError(error) });
12507
+ throw error;
12508
+ }
12509
+ }
12510
+ /** The steps of the run, until it completes, pauses, fails, or another runner takes the book. */
12511
+ async runSteps(initial) {
12512
+ const { store } = this;
12513
+ let state = initial;
12370
12514
  let conflictRetries = 0;
12371
12515
  let failureRetries = 0;
12372
12516
  let lastSuccessKey = "";
@@ -12381,13 +12525,6 @@ var AutopilotRun = class {
12381
12525
  state = await this.finish({ ...state, lastError: { message: "Too many steps in one run.", at: this.now().toISOString() } }, "error", "Too many steps in one run.");
12382
12526
  break;
12383
12527
  }
12384
- const latest = await readStoredAutopilot(store);
12385
- if (latest.state?.runner?.runId && latest.state.runner.runId !== this.runId) {
12386
- this.emit({ type: "insight", tone: "warning", code: "takenOver", title: "Another runner took over this book: this run stops." });
12387
- break;
12388
- }
12389
- if (latest.state)
12390
- state = { ...latest.state, status: "running", runner: state.runner };
12391
12528
  let task = null;
12392
12529
  const spend = newSpend();
12393
12530
  let lastLabel;
@@ -12395,7 +12532,17 @@ var AutopilotRun = class {
12395
12532
  this.stepController = stepController;
12396
12533
  const onRunAbort = () => stepController.abort(this.controller.signal.reason);
12397
12534
  this.controller.signal.addEventListener("abort", onRunAbort, { once: true });
12535
+ if (this.controller.signal.aborted)
12536
+ onRunAbort();
12398
12537
  try {
12538
+ const latest = await readStoredAutopilot(store);
12539
+ assertStoredStateReadable(latest);
12540
+ if (this.lostLease(latest)) {
12541
+ this.loseLease();
12542
+ break;
12543
+ }
12544
+ if (latest.state)
12545
+ state = { ...latest.state, status: "running", runner: state.runner };
12399
12546
  await this.config.hooks?.beforeStep?.(stepController.signal);
12400
12547
  const snapshot = await loadBookSnapshot(store, this.cache);
12401
12548
  const context = await this.stepContext(snapshot, state);
@@ -12556,6 +12703,8 @@ ${currentRequest(correction.note)}` : request2.user;
12556
12703
  if (spent)
12557
12704
  state = { ...state, stats: saved?.stats ?? withSpend(state.stats, spend) };
12558
12705
  }
12706
+ if (this.leaseLost)
12707
+ break;
12559
12708
  if (interrupted)
12560
12709
  continue;
12561
12710
  if (runAborted || isAbortError(error)) {
@@ -12590,24 +12739,44 @@ ${currentRequest(correction.note)}` : request2.user;
12590
12739
  } finally {
12591
12740
  this.controller.signal.removeEventListener("abort", onRunAbort);
12592
12741
  this.stepController = null;
12742
+ this.interrupted = false;
12593
12743
  }
12594
12744
  }
12595
- this.emit({ type: "run-ended", runId: this.runId, status: state.status, message: state.message });
12745
+ if (this.leaseLost)
12746
+ state = { ...state, status: "paused", message: TAKEN_OVER, runner: null };
12747
+ this.emit({ type: "run-ended", runId: this.runId, status: state.status, message: state.message, ...this.leaseLost ? { code: "takenOver" } : {} });
12596
12748
  return state;
12597
12749
  }
12750
+ /** Another run is recorded as the owner of the book. A stored runner of `null` means nobody owns it: this run may still record its end. */
12751
+ lostLease(stored) {
12752
+ const owner = stored.state?.runner?.runId;
12753
+ return Boolean(owner) && owner !== this.runId;
12754
+ }
12755
+ /** From now on this run writes nothing: the book belongs to another runner. */
12756
+ loseLease() {
12757
+ if (this.leaseLost)
12758
+ return;
12759
+ this.leaseLost = true;
12760
+ this.emit({ type: "insight", tone: "warning", code: "takenOver", title: TAKEN_OVER });
12761
+ }
12598
12762
  /**
12599
12763
  * Save a failed or cancelled step. What its AI calls cost is added to the state's statistics
12600
12764
  * in the same commit, so the budget limit never under-counts rejected replies.
12601
12765
  */
12602
12766
  async persistFailure(activity, spend) {
12603
12767
  for (let attempt = 0; attempt < 3; attempt += 1) {
12604
- const stored = await readStoredAutopilot(this.config.store);
12768
+ const stored = await readStoredAutopilot(this.store);
12769
+ assertStoredStateReadable(stored);
12770
+ if (this.lostLease(stored)) {
12771
+ this.loseLease();
12772
+ return null;
12773
+ }
12605
12774
  const changes = [activityChange([...stored.activities, activity], stored.activityHash)];
12606
12775
  const next = spend && stored.state ? stampState({ ...stored.state, stats: withSpend(stored.state.stats, spend) }, this.now()) : null;
12607
12776
  if (next)
12608
12777
  changes.push(stateChange(next, stored.stateHash));
12609
12778
  try {
12610
- await this.config.store.commit(changes, `Autopilot: ${activity.title} (${activity.status})`);
12779
+ await this.store.commit(changes, `Autopilot: ${activity.title} (${activity.status})`);
12611
12780
  return next;
12612
12781
  } catch (error) {
12613
12782
  if (!isBookConflictError(error))
@@ -12618,13 +12787,24 @@ ${currentRequest(correction.note)}` : request2.user;
12618
12787
  }
12619
12788
  /** Close the run with a final status, persisted together with any queued intervention. */
12620
12789
  async finish(state, status, message) {
12621
- let final = state;
12790
+ const ended = { ...state, status: state.question ? "waiting-author" : status, message, runner: null, phase: phaseOf(state) };
12791
+ let final = ended;
12622
12792
  for (let attempt = 0; attempt < 3; attempt += 1) {
12623
- const stored = await readStoredAutopilot(this.config.store);
12624
- final = { ...state, status: state.question ? "waiting-author" : status, message, runner: null, phase: phaseOf(state) };
12625
- final = stampState(this.drainQueue(final, message ?? status), this.now());
12793
+ let stored;
12626
12794
  try {
12627
- await this.config.store.commit([stateChange(final, stored.stateHash)], `Autopilot: ${final.status}`);
12795
+ stored = await readStoredAutopilot(this.store);
12796
+ } catch (error) {
12797
+ this.emit({ type: "insight", tone: "warning", code: "finalStateFailed", title: "Could not save the final autopilot state.", detail: describeError(error) });
12798
+ return final;
12799
+ }
12800
+ assertStoredStateReadable(stored);
12801
+ if (this.lostLease(stored)) {
12802
+ this.loseLease();
12803
+ return state;
12804
+ }
12805
+ final = stampState(this.drainQueue(ended, message ?? status), this.now());
12806
+ try {
12807
+ await this.store.commit([stateChange(final, stored.stateHash)], `Autopilot: ${final.status}`);
12628
12808
  this.emit({ type: "state", state: summarizeAutopilotState(final) });
12629
12809
  return final;
12630
12810
  } catch (error) {
@@ -12660,6 +12840,10 @@ var AutopilotSession = class {
12660
12840
  config;
12661
12841
  run = null;
12662
12842
  runPromise = null;
12843
+ /** A `start()` in flight: from its first line until the run has saved its start, or the start has failed. */
12844
+ starting = null;
12845
+ /** A pause asked for while a start was in flight: it is applied as soon as the run exists. */
12846
+ pendingPause = null;
12663
12847
  seq = 0;
12664
12848
  events = [];
12665
12849
  listeners = /* @__PURE__ */ new Set();
@@ -12672,8 +12856,9 @@ var AutopilotSession = class {
12672
12856
  get bookId() {
12673
12857
  return this.config.bookId;
12674
12858
  }
12859
+ /** A run is active, or a start is about to create one. */
12675
12860
  get running() {
12676
- return this.run !== null;
12861
+ return this.run !== null || this.starting !== null;
12677
12862
  }
12678
12863
  get lastSeq() {
12679
12864
  return this.seq;
@@ -12716,7 +12901,7 @@ var AutopilotSession = class {
12716
12901
  return runner.hostId && this.config.hostId ? runner.hostId === this.config.hostId : runner.label === this.config.hostLabel;
12717
12902
  }
12718
12903
  async ownRunnerAliveElsewhere() {
12719
- if (this.running || !this.config.ownRunnerAlive)
12904
+ if (this.run || !this.config.ownRunnerAlive)
12720
12905
  return false;
12721
12906
  try {
12722
12907
  return Boolean(await this.config.ownRunnerAlive());
@@ -12744,14 +12929,27 @@ var AutopilotSession = class {
12744
12929
  activities,
12745
12930
  live: this.live,
12746
12931
  running: this.running,
12747
- host: this.running ? this.config.host : state?.status === "running" ? state.runner?.host ?? null : null
12932
+ host: this.running ? this.config.host : state?.status === "running" ? state.runner?.host ?? null : null,
12933
+ ...stored.corrupt ? { stateError: { code: "state_corrupt", message: new AutopilotStateCorruptError(stored.corrupt).message } } : {}
12748
12934
  };
12749
12935
  }
12750
12936
  /** Start or resume. Resolves once the run has saved its start, rejects if it could not start. */
12751
12937
  async start(options = {}) {
12752
- if (this.run)
12938
+ if (this.running)
12753
12939
  throw new AutopilotBusyError(this.config.hostLabel);
12940
+ const starting = this.launch(options);
12941
+ this.starting = starting;
12942
+ try {
12943
+ await starting;
12944
+ } finally {
12945
+ if (this.starting === starting)
12946
+ this.starting = null;
12947
+ this.pendingPause = null;
12948
+ }
12949
+ }
12950
+ async launch(options) {
12754
12951
  const stored = await readStoredAutopilot(this.config.store);
12952
+ assertStoredStateReadable(stored);
12755
12953
  const runner = stored.state?.runner;
12756
12954
  if (!options.force && runner && runnerActivity(stored.state, this.now()) === "active" && (!this.isOwnRunner(runner) || await this.ownRunnerAliveElsewhere())) {
12757
12955
  throw new AutopilotBusyError(runner.label || runner.host);
@@ -12795,25 +12993,38 @@ var AutopilotSession = class {
12795
12993
  }
12796
12994
  });
12797
12995
  this.runPromise.catch(() => void 0);
12996
+ const pause = this.pendingPause;
12997
+ this.pendingPause = null;
12998
+ if (pause === "now")
12999
+ run.stop();
13000
+ else if (pause === "after-step")
13001
+ run.stopAfterStep();
12798
13002
  await started;
12799
13003
  }
12800
13004
  /** `now` abandons the request in flight; `after-step` lets the current step finish and save. */
12801
13005
  pause(mode = "after-step") {
12802
- if (!this.run)
13006
+ if (this.run) {
13007
+ if (mode === "now")
13008
+ this.run.stop();
13009
+ else
13010
+ this.run.stopAfterStep();
13011
+ return true;
13012
+ }
13013
+ if (!this.starting)
12803
13014
  return false;
12804
- if (mode === "now")
12805
- this.run.stop();
12806
- else
12807
- this.run.stopAfterStep();
13015
+ this.pendingPause = mode === "now" || this.pendingPause === "now" ? "now" : "after-step";
12808
13016
  return true;
12809
13017
  }
12810
- /** Resolves when no run is active. */
13018
+ /** Resolves when no run is active, and no start is about to create one. */
12811
13019
  async waitForIdle() {
13020
+ if (this.starting)
13021
+ await this.starting.catch(() => void 0);
12812
13022
  await this.runPromise?.catch(() => void 0);
12813
13023
  }
12814
13024
  async mutate(message, change) {
12815
13025
  for (let attempt = 0; attempt < 3; attempt += 1) {
12816
13026
  const stored = await readStoredAutopilot(this.config.store);
13027
+ assertStoredStateReadable(stored);
12817
13028
  const next = stampState(change(stored.state ?? createAutopilotState(this.now())), this.now());
12818
13029
  try {
12819
13030
  await this.config.store.commit([stateChange(next, stored.stateHash)], message);
@@ -12830,6 +13041,8 @@ var AutopilotSession = class {
12830
13041
  async intervene(input) {
12831
13042
  if (!input.text.trim())
12832
13043
  throw new Error("The intervention is empty.");
13044
+ if (this.starting)
13045
+ await this.starting.catch(() => void 0);
12833
13046
  if (this.run) {
12834
13047
  this.run.intervene(input);
12835
13048
  this.emit({ type: "insight", tone: "info", code: input.interrupt ? "interventionInterrupt" : "interventionQueued", title: input.interrupt ? "Intervention recorded: the current step restarts with it." : "Intervention recorded: it applies from the next step." });
@@ -12838,13 +13051,13 @@ var AutopilotSession = class {
12838
13051
  return this.mutate("Autopilot: author intervention", (state) => applyIntervention(state, input, this.now(), "paused"));
12839
13052
  }
12840
13053
  async answer(answer) {
12841
- if (this.run)
13054
+ if (this.running)
12842
13055
  throw new AutopilotBusyError(this.config.hostLabel);
12843
13056
  return this.mutate("Autopilot: author answer", (state) => applyAnswer(state, answer, this.now()));
12844
13057
  }
12845
13058
  /** Change the brief or the options while the autopilot is not running. */
12846
13059
  async configure(input) {
12847
- if (this.run)
13060
+ if (this.running)
12848
13061
  throw new AutopilotBusyError(this.config.hostLabel);
12849
13062
  return this.mutate("Autopilot: settings", (state) => ({
12850
13063
  ...state,
@@ -12854,11 +13067,11 @@ var AutopilotSession = class {
12854
13067
  }
12855
13068
  /** Forget the autopilot state (plan, history, rules). The book's chapters are never touched. */
12856
13069
  async reset() {
12857
- if (this.run)
13070
+ if (this.running)
12858
13071
  throw new AutopilotBusyError(this.config.hostLabel);
12859
- const changes = (await Promise.all([AUTOPILOT_STATE_FILE, AUTOPILOT_ACTIVITY_FILE, AUTOPILOT_PLAN_FILE].map(async (path10) => {
12860
- const file = await this.config.store.read(path10);
12861
- return file ? { path: path10, content: null, expectedHash: file.hash } : null;
13072
+ const changes = (await Promise.all([AUTOPILOT_STATE_FILE, AUTOPILOT_ACTIVITY_FILE, AUTOPILOT_PLAN_FILE].map(async (path11) => {
13073
+ const file = await this.config.store.read(path11);
13074
+ return file ? { path: path11, content: null, expectedHash: file.hash } : null;
12862
13075
  }))).filter((change) => Boolean(change));
12863
13076
  if (changes.length)
12864
13077
  await this.config.store.commit(changes, "Autopilot: reset");
@@ -13104,6 +13317,27 @@ function sanitizedNpmEnvironment(source = process.env) {
13104
13317
  for (const name of NETWORK_AND_SYSTEM_VARIABLES) if (source[name] !== void 0) env[name] = source[name];
13105
13318
  return env;
13106
13319
  }
13320
+ var NPM_PRIVATE_FOLDER = path.join("private", "npm-home");
13321
+ function npmChildEnvironment(source, home) {
13322
+ const env = {};
13323
+ for (const [name, value] of Object.entries(sanitizedNpmEnvironment(source))) if (value !== void 0 && !/^npm_config_/i.test(name)) env[name] = value;
13324
+ for (const name of ["HOMEDRIVE", "HOMEPATH", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"]) delete env[name];
13325
+ for (const name of ["HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA"]) env[name] = home;
13326
+ return env;
13327
+ }
13328
+ async function prepareNpmSandbox(dataRoot2) {
13329
+ const base = path.join(dataRoot2, NPM_PRIVATE_FOLDER);
13330
+ const home = path.join(base, "home");
13331
+ const cache = path.join(base, "cache");
13332
+ const run = path.join(base, `run-${randomBytes(8).toString("hex")}`);
13333
+ const work = path.join(run, "work");
13334
+ for (const directory of [base, home, cache, work]) await fs.mkdir(directory, { recursive: true, mode: 448 });
13335
+ if (process.platform !== "win32") await fs.chmod(base, 448);
13336
+ const userconfig = path.join(run, "userconfig");
13337
+ const globalconfig = path.join(run, "globalconfig");
13338
+ for (const file of [userconfig, globalconfig]) await fs.writeFile(file, "", { flag: "wx", mode: 384 });
13339
+ return { run, home, cache, userconfig, globalconfig, work };
13340
+ }
13107
13341
  function parseUpdateRequest(value) {
13108
13342
  if (!isPlainObject(value) || !exactKeys(value, ["schemaVersion", "packageName", "version", "requestedAt", "requestId", "source"])) throw new Error("Not a valid update request.");
13109
13343
  assertExactSemver(value.version);
@@ -13178,8 +13412,8 @@ async function writeControlJson(file, value) {
13178
13412
  }
13179
13413
  async function regularFile(file) {
13180
13414
  try {
13181
- const stat2 = await fs.lstat(file);
13182
- return stat2.isFile() && !stat2.isSymbolicLink() && stat2.nlink === 1;
13415
+ const stat = await fs.lstat(file);
13416
+ return stat.isFile() && !stat.isSymbolicLink() && stat.nlink === 1;
13183
13417
  } catch (error) {
13184
13418
  if (error.code === "ENOENT") return false;
13185
13419
  throw error;
@@ -13188,8 +13422,8 @@ async function regularFile(file) {
13188
13422
  async function ensureDataRoot(dataRoot2) {
13189
13423
  const root = path.resolve(dataRoot2);
13190
13424
  await fs.mkdir(root, { recursive: true, mode: 448 });
13191
- const stat2 = await fs.lstat(root);
13192
- if (!stat2.isDirectory() || stat2.isSymbolicLink()) throw new Error("The data folder must be a regular directory.");
13425
+ const stat = await fs.lstat(root);
13426
+ if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("The data folder must be a regular directory.");
13193
13427
  if (process.platform !== "win32") await fs.chmod(root, 448);
13194
13428
  const privateDir = path.join(root, "private");
13195
13429
  await fs.mkdir(privateDir, { recursive: true, mode: 448 });
@@ -13242,6 +13476,7 @@ var RuntimeInstaller = class {
13242
13476
  runner;
13243
13477
  npm;
13244
13478
  npmPrefix;
13479
+ environment;
13245
13480
  now;
13246
13481
  uuid;
13247
13482
  constructor(options) {
@@ -13251,13 +13486,36 @@ var RuntimeInstaller = class {
13251
13486
  const npm = options.npmBinary ? { command: options.npmBinary, prefix: [] } : npmInvocation();
13252
13487
  this.npm = npm.command;
13253
13488
  this.npmPrefix = npm.prefix;
13489
+ this.environment = options.environment ?? process.env;
13254
13490
  this.now = options.now ?? (() => /* @__PURE__ */ new Date());
13255
13491
  this.uuid = options.uuid ?? randomUUID;
13256
13492
  }
13493
+ /**
13494
+ * One npm run, in a world of its own: empty user and global configuration files, a private cache and home, a working
13495
+ * folder that is empty (and pinned as npm's prefix, or npm would take a parent folder with a `node_modules` for a project
13496
+ * and read its `.npmrc`), no `npm_config_*` variable, no install scripts. The owner's `~/.npmrc` is never read, so its
13497
+ * tokens and registry overrides cannot reach a registry. The public registry is always the last argument.
13498
+ */
13257
13499
  async runNpm(args2) {
13258
- const result = await this.runner(this.npm, [...this.npmPrefix, ...args2, "--registry=https://registry.npmjs.org"], { cwd: this.dataRoot, env: sanitizedNpmEnvironment(), timeoutMs: 10 * 6e4 });
13259
- if (result.code !== 0) throw new Error(`npm ${args2[0]} did not succeed.`);
13260
- return result;
13500
+ const sandbox = await prepareNpmSandbox(this.dataRoot);
13501
+ try {
13502
+ const flags = [
13503
+ "--userconfig",
13504
+ sandbox.userconfig,
13505
+ "--globalconfig",
13506
+ sandbox.globalconfig,
13507
+ "--cache",
13508
+ sandbox.cache,
13509
+ ...args2.includes("--prefix") ? [] : ["--prefix", sandbox.work],
13510
+ ...args2.includes("--ignore-scripts") ? [] : ["--ignore-scripts"],
13511
+ "--no-update-notifier"
13512
+ ];
13513
+ const result = await this.runner(this.npm, [...this.npmPrefix, ...args2, ...flags, "--registry=https://registry.npmjs.org"], { cwd: sandbox.work, env: npmChildEnvironment(this.environment, sandbox.home), timeoutMs: 10 * 6e4 });
13514
+ if (result.code !== 0) throw new Error(`npm ${args2[0]} did not succeed.`);
13515
+ return result;
13516
+ } finally {
13517
+ await fs.rm(sandbox.run, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }).catch(() => void 0);
13518
+ }
13261
13519
  }
13262
13520
  async latestVersion() {
13263
13521
  await ensureDataRoot(this.dataRoot);
@@ -13327,10 +13585,10 @@ var RuntimeInstaller = class {
13327
13585
  continue;
13328
13586
  }
13329
13587
  if (!entry.isFile()) throw new Error("npm package with irregular files: refused.");
13330
- const stat2 = await fs.lstat(file);
13588
+ const stat = await fs.lstat(file);
13331
13589
  files += 1;
13332
- bytes += stat2.size;
13333
- if (stat2.size > 20 * 1024 * 1024 || files > 5e3 || bytes > 80 * 1024 * 1024) throw new Error("npm package over the safety limits.");
13590
+ bytes += stat.size;
13591
+ if (stat.size > 20 * 1024 * 1024 || files > 5e3 || bytes > 80 * 1024 * 1024) throw new Error("npm package over the safety limits.");
13334
13592
  hash2.update(JSON.stringify(name));
13335
13593
  hash2.update("\0");
13336
13594
  hash2.update(await fs.readFile(file));
@@ -13395,25 +13653,30 @@ var RuntimeInstaller = class {
13395
13653
  };
13396
13654
 
13397
13655
  // src/server.ts
13398
- import http, { request as httpRequest2 } from "node:http";
13399
- import { randomUUID as randomUUID6 } from "node:crypto";
13656
+ import http, { Agent } from "node:http";
13657
+ import { randomBytes as randomBytes4, randomUUID as randomUUID8 } from "node:crypto";
13400
13658
 
13401
13659
  // src/auth.ts
13402
- import { createHash as createHash3, randomBytes as randomBytes2, randomInt, randomUUID as randomUUID3, timingSafeEqual } from "node:crypto";
13660
+ import { createHash as createHash3, randomBytes as randomBytes3, randomInt, randomUUID as randomUUID3, timingSafeEqual } from "node:crypto";
13403
13661
  import * as fs4 from "node:fs/promises";
13404
13662
  import path4 from "node:path";
13405
13663
 
13406
13664
  // src/errors.ts
13407
13665
  var HttpError = class extends Error {
13408
- constructor(status, code, message) {
13666
+ constructor(status, code, message, headers = {}) {
13409
13667
  super(message);
13410
13668
  this.status = status;
13411
13669
  this.code = code;
13670
+ this.headers = headers;
13412
13671
  this.name = "HttpError";
13413
13672
  }
13414
13673
  status;
13415
13674
  code;
13675
+ headers;
13416
13676
  };
13677
+ function retryAfter(milliseconds) {
13678
+ return { "retry-after": String(Math.max(1, Math.ceil(milliseconds / 1e3))) };
13679
+ }
13417
13680
 
13418
13681
  // src/files.ts
13419
13682
  import { spawn as spawn2 } from "node:child_process";
@@ -13506,9 +13769,9 @@ async function atomicJson(file, value, mode = 384) {
13506
13769
  async function ensurePrivateDirectory(directory) {
13507
13770
  const resolved = path2.resolve(directory);
13508
13771
  await fs2.mkdir(resolved, { recursive: true, mode: 448 });
13509
- const stat2 = await fs2.lstat(resolved);
13510
- if (!stat2.isDirectory() || stat2.isSymbolicLink()) throw new Error(`${resolved} must be a directory, not a link.`);
13511
- if (process.platform !== "win32" && (stat2.mode & 63) !== 0) await fs2.chmod(resolved, 448);
13772
+ const stat = await fs2.lstat(resolved);
13773
+ if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error(`${resolved} must be a directory, not a link.`);
13774
+ if (process.platform !== "win32" && (stat.mode & 63) !== 0) await fs2.chmod(resolved, 448);
13512
13775
  return resolved;
13513
13776
  }
13514
13777
  async function readPrivateText(file, max = 4096) {
@@ -13626,9 +13889,136 @@ async function lockDataRoot(root, onLost = () => {
13626
13889
  };
13627
13890
  }
13628
13891
 
13892
+ // src/limits.ts
13893
+ import { isIP as isIP2 } from "node:net";
13894
+ var LOCAL_SOURCE = "local";
13895
+ var MAX_LIMITER_KEYS = 1e4;
13896
+ function ipv6Groups(address) {
13897
+ let text3 = address.toLowerCase();
13898
+ const zone = text3.indexOf("%");
13899
+ if (zone >= 0) text3 = text3.slice(0, zone);
13900
+ const lastColon = text3.lastIndexOf(":");
13901
+ const tail2 = text3.slice(lastColon + 1);
13902
+ if (tail2.includes(".")) {
13903
+ const [a, b, c, d] = tail2.split(".").map(Number);
13904
+ text3 = `${text3.slice(0, lastColon + 1)}${(a << 8 | b).toString(16)}:${(c << 8 | d).toString(16)}`;
13905
+ }
13906
+ const [head, rest] = text3.split("::");
13907
+ const front = head ? head.split(":") : [];
13908
+ const back = rest ? rest.split(":") : [];
13909
+ const gap = text3.includes("::") ? 8 - front.length - back.length : 0;
13910
+ return [...front, ...Array(gap).fill("0"), ...back].map((group) => Number.parseInt(group, 16));
13911
+ }
13912
+ function mappedIpv4(groups) {
13913
+ if (!groups.slice(0, 5).every((group) => group === 0) || groups[5] !== 65535) return void 0;
13914
+ return [groups[6] >> 8, groups[6] & 255, groups[7] >> 8, groups[7] & 255].join(".");
13915
+ }
13916
+ function isLoopbackAddress(address) {
13917
+ const family = isIP2(address);
13918
+ if (family === 4) return address.startsWith("127.");
13919
+ if (family !== 6) return false;
13920
+ const groups = ipv6Groups(address);
13921
+ const mapped = mappedIpv4(groups);
13922
+ if (mapped) return mapped.startsWith("127.");
13923
+ return groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1;
13924
+ }
13925
+ function sourceFromAddress(address) {
13926
+ const family = isIP2(address);
13927
+ if (family === 4) return address;
13928
+ if (family !== 6) return void 0;
13929
+ const groups = ipv6Groups(address);
13930
+ return mappedIpv4(groups) ?? `${groups.slice(0, 4).map((group) => group.toString(16)).join(":")}::/64`;
13931
+ }
13932
+ function sourceOf(request2, trustTunnelHeader) {
13933
+ const peer = request2.socket.remoteAddress;
13934
+ if (!peer) return "unknown";
13935
+ if (!isLoopbackAddress(peer)) return sourceFromAddress(peer) ?? peer.slice(0, 64);
13936
+ if (trustTunnelHeader) {
13937
+ const claimed = request2.headers["cf-connecting-ip"];
13938
+ const source = typeof claimed === "string" ? sourceFromAddress(claimed.trim()) : void 0;
13939
+ if (source) return source;
13940
+ }
13941
+ return LOCAL_SOURCE;
13942
+ }
13943
+ var WindowLimiter = class {
13944
+ /** Oldest window first: expired windows are always a prefix. */
13945
+ windows = /* @__PURE__ */ new Map();
13946
+ max;
13947
+ windowMs;
13948
+ maxKeys;
13949
+ now;
13950
+ constructor(options) {
13951
+ this.max = options.max;
13952
+ this.windowMs = options.windowMs;
13953
+ this.maxKeys = options.maxKeys ?? MAX_LIMITER_KEYS;
13954
+ this.now = options.now ?? (() => performance.now());
13955
+ }
13956
+ get size() {
13957
+ return this.windows.size;
13958
+ }
13959
+ /** Count one hit for the key. The hit that would pass `max` is refused. */
13960
+ hit(key) {
13961
+ const now = this.now();
13962
+ let window = this.windows.get(key);
13963
+ if (window && now - window.start >= this.windowMs) {
13964
+ this.windows.delete(key);
13965
+ window = void 0;
13966
+ }
13967
+ if (!window) {
13968
+ this.makeRoom(now);
13969
+ window = { start: now, count: 0 };
13970
+ this.windows.set(key, window);
13971
+ }
13972
+ if (window.count >= this.max) return { allowed: false, retryAfterMs: Math.max(1, window.start + this.windowMs - now) };
13973
+ window.count += 1;
13974
+ return { allowed: true, retryAfterMs: 0 };
13975
+ }
13976
+ /** Forget the key (after a success). */
13977
+ reset(key) {
13978
+ this.windows.delete(key);
13979
+ }
13980
+ /** Expired windows go first, then the oldest live ones: the map never holds more than `maxKeys` keys. */
13981
+ makeRoom(now) {
13982
+ if (this.windows.size < this.maxKeys) return;
13983
+ for (const [key, window] of this.windows) {
13984
+ if (now - window.start < this.windowMs) break;
13985
+ this.windows.delete(key);
13986
+ }
13987
+ while (this.windows.size >= this.maxKeys) {
13988
+ const oldest = this.windows.keys().next();
13989
+ if (oldest.done) break;
13990
+ this.windows.delete(oldest.value);
13991
+ }
13992
+ }
13993
+ };
13994
+ var ConcurrencyLimiter = class {
13995
+ constructor(max) {
13996
+ this.max = max;
13997
+ }
13998
+ max;
13999
+ held = /* @__PURE__ */ new Map();
14000
+ get size() {
14001
+ return this.held.size;
14002
+ }
14003
+ /** Take a slot: the function that gives it back (once), or `undefined` when the key already holds `max`. */
14004
+ enter(key) {
14005
+ const count = this.held.get(key) ?? 0;
14006
+ if (count >= this.max) return void 0;
14007
+ this.held.set(key, count + 1);
14008
+ let released = false;
14009
+ return () => {
14010
+ if (released) return;
14011
+ released = true;
14012
+ const remaining = (this.held.get(key) ?? 1) - 1;
14013
+ if (remaining <= 0) this.held.delete(key);
14014
+ else this.held.set(key, remaining);
14015
+ };
14016
+ }
14017
+ };
14018
+
13629
14019
  // src/vault.ts
13630
14020
  import { spawn as spawn3 } from "node:child_process";
13631
- import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
14021
+ import { createCipheriv, createDecipheriv, randomBytes as randomBytes2 } from "node:crypto";
13632
14022
  import * as fs3 from "node:fs/promises";
13633
14023
  import path3 from "node:path";
13634
14024
  var AAD = Buffer.from("narrarium-server-v1");
@@ -13678,15 +14068,15 @@ var Vault = class _Vault {
13678
14068
  const file = path3.join(directory, "master.key");
13679
14069
  let key;
13680
14070
  try {
13681
- const stat2 = await fs3.lstat(file);
13682
- if (!stat2.isFile() || stat2.isSymbolicLink() || stat2.nlink !== 1 || process.platform !== "win32" && (stat2.mode & 63) !== 0) {
14071
+ const stat = await fs3.lstat(file);
14072
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || process.platform !== "win32" && (stat.mode & 63) !== 0) {
13683
14073
  throw new Error("The vault key file is not private (it must be a 0600 regular file).");
13684
14074
  }
13685
14075
  const stored = await fs3.readFile(file);
13686
14076
  key = process.platform === "win32" ? await dpapi(stored, false) : stored;
13687
14077
  } catch (error) {
13688
14078
  if (error.code !== "ENOENT") throw error;
13689
- key = randomBytes(32);
14079
+ key = randomBytes2(32);
13690
14080
  await fs3.writeFile(file, process.platform === "win32" ? await dpapi(key, true) : key, { flag: "wx", mode: 384, flush: true });
13691
14081
  if (process.platform !== "win32") {
13692
14082
  const handle = await fs3.open(directory, "r");
@@ -13701,7 +14091,7 @@ var Vault = class _Vault {
13701
14091
  return new _Vault(key);
13702
14092
  }
13703
14093
  encrypt(text3) {
13704
- const iv = randomBytes(12);
14094
+ const iv = randomBytes2(12);
13705
14095
  const cipher = createCipheriv("aes-256-gcm", this.key, iv);
13706
14096
  cipher.setAAD(AAD);
13707
14097
  const data2 = Buffer.concat([cipher.update(text3, "utf8"), cipher.final(), cipher.getAuthTag()]);
@@ -13722,13 +14112,17 @@ var Vault = class _Vault {
13722
14112
 
13723
14113
  // src/auth.ts
13724
14114
  var hash = (value) => createHash3("sha256").update(value).digest("hex");
13725
- var secret = () => randomBytes2(32).toString("base64url");
14115
+ var secret = () => randomBytes3(32).toString("base64url");
13726
14116
  var sameHash = (left, right) => left.length === right.length && timingSafeEqual(Buffer.from(left), Buffer.from(right));
14117
+ var sameSecret = (left, right) => sameHash(hash(left), hash(right));
13727
14118
  var MAX_CLIENTS = 100;
13728
14119
  var ADMIN_SESSION_MS = 30 * 6e4;
13729
14120
  var PAIRING_MS = 5 * 6e4;
13730
- var ATTEMPTS_PER_WINDOW = 20;
13731
14121
  var ATTEMPT_WINDOW_MS = 5 * 6e4;
14122
+ var LOGIN_FAILURES_PER_WINDOW = 10;
14123
+ var PAIRING_ATTEMPTS_PER_WINDOW = 10;
14124
+ var PAIRING_GUESSES_PER_SOURCE = 5;
14125
+ var PAIRING_GUESSES_TOTAL = 50;
13732
14126
  var ADMIN_CODE_FILE = "admin-code.txt";
13733
14127
  function isScope(value) {
13734
14128
  return typeof value === "string" && SERVER_SCOPES.includes(value);
@@ -13740,6 +14134,12 @@ function parseScopes(value) {
13740
14134
  }
13741
14135
  return scopes;
13742
14136
  }
14137
+ function assertScope(client, scope) {
14138
+ if (!client.scopes.includes(scope)) throw new HttpError(403, "scope", `This client token does not have the "${scope}" permission.`);
14139
+ }
14140
+ function tooManyAttempts(retryAfterMs) {
14141
+ return new HttpError(429, "too_many_attempts", "Too many attempts from this address: wait a few minutes and try again.", retryAfter(retryAfterMs));
14142
+ }
13743
14143
  function assertAdminCode(value) {
13744
14144
  if (value.length < 32 || value.length > 200 || value !== value.trim() || /[\u0000-\u001f\u007f]/u.test(value)) {
13745
14145
  throw new HttpError(422, "admin_code", "The admin code must have 32-200 visible characters, without line breaks or leading/trailing spaces.");
@@ -13765,9 +14165,7 @@ function parseState(value) {
13765
14165
  version: 1,
13766
14166
  adminHash: record2.adminHash,
13767
14167
  clients,
13768
- ...pairing ? { pairing: { ...pairing, scopes: pairing.scopes.filter(isScope) } } : {},
13769
- failureWindow: typeof record2.failureWindow === "number" ? record2.failureWindow : Date.now(),
13770
- failures: typeof record2.failures === "number" ? record2.failures : 0
14168
+ ...pairing ? { pairing: { ...pairing, scopes: pairing.scopes.filter(isScope) } } : {}
13771
14169
  };
13772
14170
  }
13773
14171
  async function adminCodeFromFile(file) {
@@ -13794,6 +14192,11 @@ var Auth = class _Auth {
13794
14192
  sessions = /* @__PURE__ */ new Map();
13795
14193
  lastSeen = /* @__PURE__ */ new Map();
13796
14194
  tail = Promise.resolve();
14195
+ /** Wrong admin codes per source. In memory only: nothing anonymous is ever written to disk. */
14196
+ logins = new WindowLimiter({ max: LOGIN_FAILURES_PER_WINDOW, windowMs: ATTEMPT_WINDOW_MS });
14197
+ pairingAttempts = new WindowLimiter({ max: PAIRING_ATTEMPTS_PER_WINDOW, windowMs: ATTEMPT_WINDOW_MS });
14198
+ /** Wrong codes per source inside the current pairing: `PAIRING_GUESSES_TOTAL` at most in all, so it stays small. */
14199
+ guessing;
13797
14200
  get statePath() {
13798
14201
  return path4.join(this.root, "private", "authorization.json");
13799
14202
  }
@@ -13806,12 +14209,12 @@ var Auth = class _Auth {
13806
14209
  const stored = await readJson(statePath, 1024 * 1024);
13807
14210
  let state;
13808
14211
  if (stored === void 0) {
13809
- state = { version: 1, adminHash: hash(code), clients: [], failureWindow: Date.now(), failures: 0 };
14212
+ state = { version: 1, adminHash: hash(code), clients: [] };
13810
14213
  await atomicJson(statePath, state);
13811
14214
  } else {
13812
14215
  state = parseState(stored);
13813
14216
  if (state.adminHash !== hash(code)) {
13814
- state = { ...state, adminHash: hash(code), failures: 0, failureWindow: Date.now() };
14217
+ state = { ...state, adminHash: hash(code) };
13815
14218
  await atomicJson(statePath, state);
13816
14219
  }
13817
14220
  }
@@ -13837,8 +14240,6 @@ var Auth = class _Auth {
13837
14240
  try {
13838
14241
  await this.change((state) => {
13839
14242
  state.adminHash = hash(code);
13840
- state.failures = 0;
13841
- state.failureWindow = Date.now();
13842
14243
  });
13843
14244
  this.sessions.clear();
13844
14245
  } catch (error) {
@@ -13851,31 +14252,29 @@ var Auth = class _Auth {
13851
14252
  await this.replaceAdminCode(code);
13852
14253
  return code;
13853
14254
  }
13854
- countAttempt(state) {
13855
- if (Date.now() - state.failureWindow > ATTEMPT_WINDOW_MS) {
13856
- state.failureWindow = Date.now();
13857
- state.failures = 0;
13858
- }
13859
- if (state.failures >= ATTEMPTS_PER_WINDOW) return false;
13860
- state.failures += 1;
13861
- return true;
13862
- }
13863
14255
  /** The client owning this token, optionally checking a scope. */
13864
14256
  authenticate(token, scope) {
13865
14257
  if (!token || token.length > 200) throw new HttpError(401, "unauthorized", "Missing or invalid client token. Pair this client again or create a token with: npx narrarium-server client-token");
13866
14258
  const digest = hash(token.trim());
13867
14259
  const client = this.state.clients.find((entry) => !entry.revoked && sameHash(entry.hash, digest));
13868
14260
  if (!client) throw new HttpError(401, "unauthorized", "This client token is not valid or was revoked. Pair this client again.");
13869
- if (scope && !client.scopes.includes(scope)) throw new HttpError(403, "scope", `This client token does not have the "${scope}" permission.`);
14261
+ if (scope) assertScope(client, scope);
13870
14262
  this.lastSeen.set(client.id, (/* @__PURE__ */ new Date()).toISOString());
13871
14263
  return structuredClone(client);
13872
14264
  }
13873
14265
  isCurrent(client) {
13874
14266
  return this.state.clients.some((entry) => entry.id === client.id && entry.hash === client.hash && !entry.revoked);
13875
14267
  }
13876
- async login(code) {
13877
- const accepted = await this.change((state) => this.countAttempt(state) && sameHash(state.adminHash, hash(code)));
13878
- if (!accepted) throw new HttpError(401, "admin_login", "Wrong admin code, or too many attempts: wait a few minutes.");
14268
+ /**
14269
+ * Check the admin code. Every source has its own budget of wrong codes: an anonymous caller can only spend its own, a
14270
+ * correct code clears the source's count, and a source over its budget is refused (429) even with the right code, so
14271
+ * the budget cannot be used to guess. Nothing is written to disk.
14272
+ */
14273
+ async login(code, source = LOCAL_SOURCE) {
14274
+ const verdict = this.logins.hit(source);
14275
+ if (!verdict.allowed) throw tooManyAttempts(verdict.retryAfterMs);
14276
+ if (!sameHash(this.state.adminHash, hash(code))) throw new HttpError(401, "admin_login", "Wrong admin code.");
14277
+ this.logins.reset(source);
13879
14278
  const cookie = secret();
13880
14279
  const csrf = secret();
13881
14280
  if (this.sessions.size >= 20) this.sessions.clear();
@@ -13885,7 +14284,7 @@ var Auth = class _Auth {
13885
14284
  adminSession(cookie, csrf) {
13886
14285
  const session = cookie ? this.sessions.get(cookie) : void 0;
13887
14286
  if (!session || session.until < Date.now()) throw new HttpError(401, "admin_session", "Sign in to the admin panel again.");
13888
- if (csrf !== void 0 && !sameHash(session.csrf, csrf)) throw new HttpError(403, "csrf", "This admin request is not authorized.");
14287
+ if (csrf !== void 0 && !sameSecret(session.csrf, csrf)) throw new HttpError(403, "csrf", "This admin request is not authorized.");
13889
14288
  }
13890
14289
  logout(cookie) {
13891
14290
  if (cookie) this.sessions.delete(cookie);
@@ -13934,21 +14333,47 @@ var Auth = class _Auth {
13934
14333
  return this.change((state) => {
13935
14334
  const code = String(randomInt(1e8, 1e9));
13936
14335
  state.pairing = { id: randomUUID3(), hash: hash(code), expiresAt: Date.now() + PAIRING_MS, attempts: 0, scopes: ["read", "write", "run"] };
14336
+ this.guessing = void 0;
13937
14337
  return { code, expiresAt: state.pairing.expiresAt };
13938
14338
  });
13939
14339
  }
13940
- async requestPairing(code, nonce, label) {
14340
+ /**
14341
+ * Count one pairing request of a source against its own budget (`PAIRING_ATTEMPTS_PER_WINDOW` per window). Every request
14342
+ * counts, also one that is malformed or comes while no pairing is open, and nothing but the source's own budget is spent.
14343
+ */
14344
+ admitPairingAttempt(source = LOCAL_SOURCE) {
14345
+ const verdict = this.pairingAttempts.hit(source);
14346
+ if (!verdict.allowed) throw tooManyAttempts(verdict.retryAfterMs);
14347
+ }
14348
+ guessesBy(pairingId, source) {
14349
+ return this.guessing?.pairingId === pairingId ? this.guessing.bySource.get(source) ?? 0 : 0;
14350
+ }
14351
+ noteGuess(pairingId, source) {
14352
+ if (this.guessing?.pairingId !== pairingId) this.guessing = { pairingId, bySource: /* @__PURE__ */ new Map() };
14353
+ this.guessing.bySource.set(source, (this.guessing.bySource.get(source) ?? 0) + 1);
14354
+ }
14355
+ /**
14356
+ * Offer the code of the open pairing. A source has `PAIRING_GUESSES_PER_SOURCE` wrong guesses in a pairing and is then
14357
+ * refused for the rest of it; wrong guesses from all sources together cancel the pairing at `PAIRING_GUESSES_TOTAL`.
14358
+ * Nothing happens (and nothing is written) when no pairing takes a code.
14359
+ */
14360
+ async requestPairing(code, nonce, label, source = LOCAL_SOURCE) {
14361
+ const invalid = () => new HttpError(401, "pairing_invalid", "The code is wrong, expired or already used, or there were too many attempts.");
14362
+ if (!takesCode(this.state.pairing)) throw invalid();
13941
14363
  const id = await this.change((state) => {
13942
- if (!this.countAttempt(state)) return void 0;
13943
14364
  const pairing = state.pairing;
13944
- if (!pairing || pairing.expiresAt < Date.now() || pairing.attempts >= 5 || pairing.requestHash) return void 0;
13945
- pairing.attempts += 1;
13946
- if (!sameHash(pairing.hash, hash(code))) return void 0;
14365
+ if (!takesCode(pairing) || this.guessesBy(pairing.id, source) >= PAIRING_GUESSES_PER_SOURCE) return void 0;
14366
+ if (!sameHash(pairing.hash, hash(code))) {
14367
+ this.noteGuess(pairing.id, source);
14368
+ pairing.attempts += 1;
14369
+ if (pairing.attempts >= PAIRING_GUESSES_TOTAL) delete state.pairing;
14370
+ return void 0;
14371
+ }
13947
14372
  pairing.requestHash = hash(nonce);
13948
14373
  pairing.label = label;
13949
14374
  return pairing.id;
13950
14375
  });
13951
- if (!id) throw new HttpError(401, "pairing_invalid", "The code is wrong, expired or already used, or there were too many attempts.");
14376
+ if (!id) throw invalid();
13952
14377
  return { id };
13953
14378
  }
13954
14379
  /** The owner approves the pending request; `allowUpdate` also lets the client update the server. */
@@ -13970,11 +14395,14 @@ var Auth = class _Auth {
13970
14395
  });
13971
14396
  }
13972
14397
  async pollPairing(id, nonce) {
14398
+ const invalid = () => new HttpError(401, "pairing_invalid", "This pairing is not valid or expired.");
14399
+ const matches = (pairing) => pairing !== void 0 && pairing.id === id && pairing.expiresAt >= Date.now() && pairing.requestHash !== void 0 && sameHash(pairing.requestHash, hash(nonce));
14400
+ const current = this.state.pairing;
14401
+ if (!matches(current)) throw invalid();
14402
+ if (!current.issued) return { pending: true };
13973
14403
  return this.change((state) => {
13974
14404
  const pairing = state.pairing;
13975
- if (!pairing || pairing.id !== id || pairing.expiresAt < Date.now() || !pairing.requestHash || !sameHash(pairing.requestHash, hash(nonce))) {
13976
- throw new HttpError(401, "pairing_invalid", "This pairing is not valid or expired.");
13977
- }
14405
+ if (!matches(pairing)) throw invalid();
13978
14406
  if (!pairing.issued) return { pending: true };
13979
14407
  const token = this.vault.decrypt(pairing.issued);
13980
14408
  delete state.pairing;
@@ -13982,17 +14410,274 @@ var Auth = class _Auth {
13982
14410
  });
13983
14411
  }
13984
14412
  };
14413
+ function takesCode(pairing) {
14414
+ return pairing !== void 0 && pairing.expiresAt >= Date.now() && !pairing.requestHash;
14415
+ }
13985
14416
 
13986
14417
  // src/books.ts
14418
+ import { randomUUID as randomUUID6 } from "node:crypto";
13987
14419
  import { existsSync as existsSync2 } from "node:fs";
13988
- import { mkdir as mkdir6, rm as rm5 } from "node:fs/promises";
13989
- import path7 from "node:path";
14420
+ import { mkdir as mkdir7, readdir as readdir4, rm as rm5 } from "node:fs/promises";
14421
+ import path8 from "node:path";
13990
14422
 
13991
14423
  // src/config.ts
13992
- import { createHash as createHash4, createPrivateKey, generateKeyPairSync, randomUUID as randomUUID4, sign } from "node:crypto";
14424
+ import { createHash as createHash5, createPrivateKey, generateKeyPairSync, randomUUID as randomUUID5, sign } from "node:crypto";
13993
14425
  import * as fs5 from "node:fs/promises";
13994
14426
  import os from "node:os";
14427
+ import path6 from "node:path";
14428
+
14429
+ // src/fs-store.ts
14430
+ import { createHash as createHash4, randomUUID as randomUUID4 } from "node:crypto";
14431
+ import { constants as constants3 } from "node:fs";
14432
+ import { lstat as lstat4, mkdir as mkdir4, open as open4, readdir as readdir2, realpath as realpath2, rm as rm3, writeFile as writeFile3 } from "node:fs/promises";
13995
14433
  import path5 from "node:path";
14434
+ var SKIPPED_DIRECTORIES = /* @__PURE__ */ new Set([".git", "node_modules", ".astro", "dist"]);
14435
+ var MAX_FILE_BYTES = 8 * 1024 * 1024;
14436
+ var MAX_COMMIT_CHANGES = 5e3;
14437
+ var MAX_ROLLBACK_BYTES = 64 * 1024 * 1024;
14438
+ var NO_FOLLOW = constants3.O_NOFOLLOW ?? 0;
14439
+ function hashText(text3) {
14440
+ return createHash4("sha256").update(text3, "utf8").digest("hex");
14441
+ }
14442
+ function unsafePath(relative) {
14443
+ return new HttpError(400, "invalid_path", `Unsafe book path: ${String(relative).slice(0, 200)}`);
14444
+ }
14445
+ var KeyedMutex = class {
14446
+ tails = /* @__PURE__ */ new Map();
14447
+ run(key, action) {
14448
+ const previous = this.tails.get(key) ?? Promise.resolve();
14449
+ const next = previous.then(action);
14450
+ const tail2 = next.then(() => void 0, () => void 0);
14451
+ this.tails.set(key, tail2);
14452
+ void tail2.then(() => {
14453
+ if (this.tails.get(key) === tail2) this.tails.delete(key);
14454
+ });
14455
+ return next;
14456
+ }
14457
+ };
14458
+ var FileBookStore = class {
14459
+ constructor(root, options = {}) {
14460
+ this.root = root;
14461
+ this.options = options;
14462
+ }
14463
+ root;
14464
+ options;
14465
+ hashes = /* @__PURE__ */ new Map();
14466
+ mutex = new KeyedMutex();
14467
+ safePath(relative) {
14468
+ try {
14469
+ return assertSafeBookPath(relative);
14470
+ } catch {
14471
+ throw unsafePath(relative);
14472
+ }
14473
+ }
14474
+ /**
14475
+ * Checks every existing component below the root with `lstat` (a link anywhere makes the path unsafe)
14476
+ * and compares the real path of the deepest existing folder with the real root.
14477
+ */
14478
+ async locate(relative) {
14479
+ const safe = this.safePath(relative);
14480
+ const root = path5.resolve(this.root);
14481
+ const parts = safe.split("/");
14482
+ const full = path5.join(root, ...parts);
14483
+ const blocked = { safe, full, unsafe: true, kind: "other" };
14484
+ if (!full.startsWith(root + path5.sep)) return blocked;
14485
+ let realRoot;
14486
+ try {
14487
+ realRoot = await realpath2(root);
14488
+ } catch (error) {
14489
+ return error.code === "ENOENT" ? { safe, full, unsafe: false, kind: "missing" } : blocked;
14490
+ }
14491
+ let anchor = root;
14492
+ let current = root;
14493
+ let kind = "missing";
14494
+ let info;
14495
+ for (let index = 0; index < parts.length; index += 1) {
14496
+ current = path5.join(current, parts[index]);
14497
+ let stats;
14498
+ try {
14499
+ stats = await lstat4(current);
14500
+ } catch (error) {
14501
+ if (error.code === "ENOENT") break;
14502
+ return blocked;
14503
+ }
14504
+ if (stats.isSymbolicLink()) return blocked;
14505
+ if (index === parts.length - 1) {
14506
+ kind = stats.isFile() ? "file" : "other";
14507
+ info = stats;
14508
+ } else if (stats.isDirectory()) {
14509
+ anchor = current;
14510
+ } else {
14511
+ kind = "other";
14512
+ break;
14513
+ }
14514
+ }
14515
+ try {
14516
+ const real = anchor === root ? realRoot : await realpath2(anchor);
14517
+ if (real !== realRoot && !real.startsWith(realRoot + path5.sep)) return blocked;
14518
+ } catch {
14519
+ return blocked;
14520
+ }
14521
+ return { safe, full, unsafe: false, kind, info: kind === "file" ? info : void 0 };
14522
+ }
14523
+ /** The bytes of a regular file of at most `MAX_FILE_BYTES`, opened without following a link. */
14524
+ async readBytes(full) {
14525
+ let handle;
14526
+ try {
14527
+ handle = await open4(full, constants3.O_RDONLY | NO_FOLLOW);
14528
+ const info = await handle.stat();
14529
+ if (!info.isFile() || info.size > MAX_FILE_BYTES) return null;
14530
+ const bytes = await handle.readFile();
14531
+ return bytes.byteLength > MAX_FILE_BYTES ? null : { bytes, info };
14532
+ } catch {
14533
+ return null;
14534
+ } finally {
14535
+ await handle?.close().catch(() => void 0);
14536
+ }
14537
+ }
14538
+ async hashOf(location) {
14539
+ const { info } = location;
14540
+ if (location.unsafe || location.kind !== "file" || !info || info.size > MAX_FILE_BYTES) return null;
14541
+ const cached = this.hashes.get(location.safe);
14542
+ if (cached && cached.mtimeMs === info.mtimeMs && cached.size === info.size) return cached.hash;
14543
+ const file = await this.readBytes(location.full);
14544
+ if (!file) return null;
14545
+ const hash2 = hashText(file.bytes.toString("utf8"));
14546
+ this.hashes.set(location.safe, { mtimeMs: file.info.mtimeMs, size: file.info.size, hash: hash2 });
14547
+ return hash2;
14548
+ }
14549
+ async list() {
14550
+ const entries = [];
14551
+ const walk = async (directory, prefix) => {
14552
+ let children;
14553
+ try {
14554
+ children = await readdir2(directory, { withFileTypes: true });
14555
+ } catch {
14556
+ return;
14557
+ }
14558
+ for (const child of children) {
14559
+ if (child.isSymbolicLink()) continue;
14560
+ const relative = prefix ? `${prefix}/${child.name}` : child.name;
14561
+ const full = path5.join(directory, child.name);
14562
+ if (child.isDirectory()) {
14563
+ if (!SKIPPED_DIRECTORIES.has(child.name)) await walk(full, relative);
14564
+ } else if (child.isFile() && isTextBookPath(relative) && isSafeBookPath(relative)) {
14565
+ const info = await lstat4(full).catch(() => null);
14566
+ if (!info?.isFile()) continue;
14567
+ const hash2 = await this.hashOf({ safe: relative, full, unsafe: false, kind: "file", info });
14568
+ if (hash2) entries.push({ path: relative, hash: hash2 });
14569
+ }
14570
+ }
14571
+ };
14572
+ await walk(path5.resolve(this.root), "");
14573
+ return entries.sort((left, right) => left.path.localeCompare(right.path));
14574
+ }
14575
+ async read(relative) {
14576
+ const location = await this.locate(relative);
14577
+ if (location.unsafe || location.kind !== "file" || !location.info || location.info.size > MAX_FILE_BYTES) return null;
14578
+ const file = await this.readBytes(location.full);
14579
+ if (!file) return null;
14580
+ const content = file.bytes.toString("utf8");
14581
+ const hash2 = hashText(content);
14582
+ this.hashes.set(location.safe, { mtimeMs: file.info.mtimeMs, size: file.info.size, hash: hash2 });
14583
+ return { path: relative, content, hash: hash2 };
14584
+ }
14585
+ async commit(changes, message) {
14586
+ const prepared = this.prepare(changes);
14587
+ return this.mutex.run("write", () => this.apply(prepared, message));
14588
+ }
14589
+ /** Everything that can be checked without looking at the disk: count, paths (normalized once), duplicates, sizes. */
14590
+ prepare(changes) {
14591
+ if (changes.length > MAX_COMMIT_CHANGES) throw new HttpError(413, "too_many_changes", `A commit may change at most ${MAX_COMMIT_CHANGES} files: send the rest in another commit.`);
14592
+ const seen = /* @__PURE__ */ new Set();
14593
+ return changes.map((change) => {
14594
+ const safe = this.safePath(change.path);
14595
+ if (!isTextBookPath(safe)) throw new HttpError(400, "invalid_path", `Only text files of a book (.md, .markdown, .txt, .json, .yaml, .yml) can be written: ${safe}`);
14596
+ const key = safe.normalize("NFC").toLowerCase();
14597
+ if (seen.has(key)) throw new HttpError(400, "duplicate_path", `The same file appears twice in one commit: ${safe}`);
14598
+ seen.add(key);
14599
+ if (change.content !== null && Buffer.byteLength(change.content, "utf8") > MAX_FILE_BYTES) throw new HttpError(413, "file_too_large", `File too large: ${safe}`);
14600
+ return { path: safe, content: change.content, expectedHash: change.expectedHash };
14601
+ });
14602
+ }
14603
+ async apply(prepared, message) {
14604
+ const targets = [];
14605
+ for (const change of prepared) {
14606
+ const location = await this.locate(change.path);
14607
+ if (location.unsafe) throw unsafePath(change.path);
14608
+ if (location.kind === "other") throw new HttpError(400, "invalid_path", `Not a file path (a folder or a file is in the way): ${change.path}`);
14609
+ targets.push({ ...change, location, original: null });
14610
+ }
14611
+ for (const target2 of targets) {
14612
+ if (target2.expectedHash === void 0) continue;
14613
+ const current = await this.hashOf(target2.location);
14614
+ if (current !== target2.expectedHash) throw new BookConflictError(`File changed since it was read: ${target2.path}`, target2.path);
14615
+ }
14616
+ let remembered = 0;
14617
+ for (const target2 of targets) {
14618
+ if (target2.location.kind !== "file") continue;
14619
+ const size = target2.location.info?.size ?? 0;
14620
+ if (size > MAX_FILE_BYTES) throw new HttpError(413, "file_too_large", `File too large to replace safely: ${target2.path}`);
14621
+ remembered += size;
14622
+ if (remembered > MAX_ROLLBACK_BYTES) throw new HttpError(413, "payload_too_large", "This commit replaces too much existing content at once: send it in smaller commits.");
14623
+ const file = await this.readBytes(target2.location.full);
14624
+ if (!file) throw unsafePath(target2.path);
14625
+ target2.original = file.bytes;
14626
+ }
14627
+ let reached = 0;
14628
+ const created = [];
14629
+ try {
14630
+ for (const target2 of targets) {
14631
+ reached += 1;
14632
+ this.hashes.delete(target2.path);
14633
+ if (target2.content === null) {
14634
+ await rm3(target2.location.full, { force: true });
14635
+ } else {
14636
+ const first = await mkdir4(path5.dirname(target2.location.full), { recursive: true });
14637
+ if (first) created.push(first);
14638
+ await this.replaceFile(target2.location.full, target2.content);
14639
+ }
14640
+ }
14641
+ const revision = await this.options.afterWrite?.(targets.map((target2) => target2.path), message);
14642
+ return { revision };
14643
+ } catch (error) {
14644
+ await this.putBack(targets.slice(0, reached), created);
14645
+ throw error;
14646
+ }
14647
+ }
14648
+ /** Write through a private temporary file in the same folder, then rename: readers never see a partial file. */
14649
+ async replaceFile(full, content) {
14650
+ const temporary = path5.join(path5.dirname(full), `.${randomUUID4()}.tmp`);
14651
+ try {
14652
+ await writeFile3(temporary, content, { flag: "wx", mode: 420 });
14653
+ await renameReplacing(temporary, full);
14654
+ } catch (error) {
14655
+ await rm3(temporary, { force: true }).catch(() => void 0);
14656
+ throw error;
14657
+ }
14658
+ }
14659
+ /** Undo a failed commit: every file back to what it held, the folders it created removed. */
14660
+ async putBack(targets, created) {
14661
+ for (const target2 of targets) {
14662
+ this.hashes.delete(target2.path);
14663
+ try {
14664
+ const location = await this.locate(target2.path);
14665
+ if (location.unsafe) continue;
14666
+ if (target2.original === null) await rm3(location.full, { force: true });
14667
+ else await this.replaceFile(location.full, target2.original);
14668
+ } catch {
14669
+ }
14670
+ }
14671
+ for (const directory of created.reverse()) await rm3(directory, { recursive: true, force: true }).catch(() => void 0);
14672
+ }
14673
+ /** Revision of the current content when the store has no git history. */
14674
+ async contentRevision() {
14675
+ const entries = await this.list();
14676
+ return hashText(entries.map((entry) => `${entry.path}:${entry.hash}`).join("\n")).slice(0, 16);
14677
+ }
14678
+ };
14679
+
14680
+ // src/config.ts
13996
14681
  var DEFAULT_ORIGINS = [
13997
14682
  "https://narrarium.net",
13998
14683
  "https://www.narrarium.net",
@@ -14002,10 +14687,18 @@ var DEFAULT_ORIGINS = [
14002
14687
  "http://127.0.0.1:4173"
14003
14688
  ];
14004
14689
  var BOOK_ID = /^[A-Za-z0-9._-]{1,120}$/;
14690
+ var TRASH_DIRECTORY = ".trash";
14691
+ var TRASH_RETENTION_MS = 14 * 24 * 60 * 60 * 1e3;
14692
+ var MAX_BOOK_TOKEN_CHARACTERS = 512;
14005
14693
  function assertBookId(bookId) {
14006
- if (!BOOK_ID.test(bookId) || bookId === "." || bookId === "..") throw new HttpError(400, "invalid_book", "Invalid book id.");
14694
+ if (!BOOK_ID.test(bookId) || bookId === "." || bookId === ".." || bookId.toLowerCase() === TRASH_DIRECTORY) throw new HttpError(400, "invalid_book", "Invalid book id.");
14007
14695
  return bookId;
14008
14696
  }
14697
+ function assertBookToken(token) {
14698
+ if (typeof token === "string" && token.trim().length > MAX_BOOK_TOKEN_CHARACTERS) {
14699
+ throw new HttpError(400, "invalid_token", `The GitHub token is too long: at most ${MAX_BOOK_TOKEN_CHARACTERS} characters.`);
14700
+ }
14701
+ }
14009
14702
  function validPort(value, fallback) {
14010
14703
  return typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 65535 ? value : fallback;
14011
14704
  }
@@ -14026,13 +14719,13 @@ function normalizeOrigin(value, allowLoopbackHttp = true) {
14026
14719
  return url.origin;
14027
14720
  }
14028
14721
  function configPath(root) {
14029
- return path5.join(root, "config.json");
14722
+ return path6.join(root, "config.json");
14030
14723
  }
14031
14724
  function parseConfig(stored) {
14032
14725
  const value = stored && typeof stored === "object" ? stored : {};
14033
14726
  return {
14034
14727
  version: 2,
14035
- serverId: typeof value.serverId === "string" && /^[0-9a-f-]{36}$/i.test(value.serverId) ? value.serverId : randomUUID4(),
14728
+ serverId: typeof value.serverId === "string" && /^[0-9a-f-]{36}$/i.test(value.serverId) ? value.serverId : randomUUID5(),
14036
14729
  label: typeof value.label === "string" && value.label.trim() ? value.label.trim().slice(0, 80) : `narrarium-server@${os.hostname()}`,
14037
14730
  apiPort: validPort(value.apiPort ?? value.port, DEFAULT_SERVER_PORT),
14038
14731
  adminPort: validPort(value.adminPort, DEFAULT_ADMIN_PORT),
@@ -14070,13 +14763,15 @@ var DataDirectory = class _DataDirectory {
14070
14763
  currentConfig;
14071
14764
  publicKey;
14072
14765
  privateKey;
14766
+ /** One writer per file: every update reads the current content inside its turn, and readers wait for a write in progress. */
14767
+ locks = new KeyedMutex();
14073
14768
  static async open(root, overrides = {}) {
14074
14769
  const resolved = await ensurePrivateDirectory(root);
14075
- await ensurePrivateDirectory(path5.join(resolved, "private"));
14076
- await fs5.mkdir(path5.join(resolved, "books"), { recursive: true, mode: 448 });
14770
+ await ensurePrivateDirectory(path6.join(resolved, "private"));
14771
+ await fs5.mkdir(path6.join(resolved, "books"), { recursive: true, mode: 448 });
14077
14772
  const vault = await Vault.open(resolved);
14078
14773
  const config = await loadServerConfig(resolved, overrides);
14079
- const identityPath = path5.join(resolved, "private", "identity.json");
14774
+ const identityPath = path6.join(resolved, "private", "identity.json");
14080
14775
  const stored = await readJson(identityPath, 64 * 1024);
14081
14776
  let identity;
14082
14777
  if (stored?.version === 1 && typeof stored.publicKey === "string" && isEncryptedSecret(stored.privateKey)) {
@@ -14097,193 +14792,147 @@ var DataDirectory = class _DataDirectory {
14097
14792
  return this.currentConfig;
14098
14793
  }
14099
14794
  get fingerprint() {
14100
- return createHash4("sha256").update(Buffer.from(this.publicKey, "base64")).digest("hex");
14795
+ return createHash5("sha256").update(Buffer.from(this.publicKey, "base64")).digest("hex");
14101
14796
  }
14102
14797
  /** Ed25519 signature proving the server's identity for one nonce. */
14103
14798
  signIdentity(nonce) {
14104
14799
  return sign(null, Buffer.from(serverIdentityMessage(nonce, this.currentConfig.serverId, this.publicKey)), this.privateKey).toString("base64");
14105
14800
  }
14106
- async updateConfig(change) {
14107
- const next = structuredClone(this.currentConfig);
14108
- change(next);
14109
- const checked = parseConfig(next);
14110
- await atomicJson(configPath(this.root), checked, 384);
14111
- this.currentConfig = checked;
14112
- return checked;
14801
+ /** Apply `change` to the configuration as it is when this update gets its turn: updates never overwrite each other. */
14802
+ updateConfig(change) {
14803
+ return this.locks.run("config", async () => {
14804
+ const next = structuredClone(this.currentConfig);
14805
+ change(next);
14806
+ const checked = parseConfig(next);
14807
+ await atomicJson(configPath(this.root), checked, 384);
14808
+ this.currentConfig = checked;
14809
+ return checked;
14810
+ });
14113
14811
  }
14114
14812
  file(...parts) {
14115
- return path5.join(this.root, ...parts);
14813
+ return path6.join(this.root, ...parts);
14116
14814
  }
14117
14815
  bookDirectory(bookId) {
14118
- return path5.join(this.root, "books", assertBookId(bookId), "repo");
14816
+ return path6.join(this.root, "books", assertBookId(bookId), "repo");
14119
14817
  }
14120
- async aiConfig() {
14121
- const stored = await readJson(this.file("private", "ai.json"), 4 * 1024 * 1024);
14122
- if (!stored || !isEncryptedSecret(stored.encrypted)) return { config: pickAiRuntimeConfig({}), updatedAt: null };
14123
- return { config: pickAiRuntimeConfig(JSON.parse(this.vault.decrypt(stored.encrypted))), updatedAt: typeof stored.updatedAt === "string" ? stored.updatedAt : null };
14818
+ aiConfig() {
14819
+ return this.locks.run("ai", async () => {
14820
+ const stored = await readJson(this.file("private", "ai.json"), 4 * 1024 * 1024);
14821
+ if (!stored || !isEncryptedSecret(stored.encrypted)) return { config: pickAiRuntimeConfig({}), updatedAt: null };
14822
+ return { config: pickAiRuntimeConfig(JSON.parse(this.vault.decrypt(stored.encrypted))), updatedAt: typeof stored.updatedAt === "string" ? stored.updatedAt : null };
14823
+ });
14124
14824
  }
14125
14825
  async saveAiConfig(config) {
14126
- await atomicJson(this.file("private", "ai.json"), { encrypted: this.vault.encrypt(JSON.stringify(pickAiRuntimeConfig(config))), updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
14826
+ await this.locks.run("ai", () => atomicJson(this.file("private", "ai.json"), { encrypted: this.vault.encrypt(JSON.stringify(pickAiRuntimeConfig(config))), updatedAt: (/* @__PURE__ */ new Date()).toISOString() }));
14127
14827
  }
14128
- async books() {
14828
+ async readBooks() {
14129
14829
  const stored = await readJson(this.file("books.json"), 4 * 1024 * 1024);
14130
14830
  return Array.isArray(stored?.books) ? stored.books.filter((book) => book && typeof book.bookId === "string" && BOOK_ID.test(book.bookId)) : [];
14131
14831
  }
14832
+ books() {
14833
+ return this.locks.run("books", () => this.readBooks());
14834
+ }
14132
14835
  async saveBook(book) {
14133
- const books = (await this.books()).filter((entry) => entry.bookId !== book.bookId);
14134
- books.push(book);
14135
- await atomicJson(this.file("books.json"), { books: books.sort((left, right) => left.title.localeCompare(right.title)) }, 384);
14836
+ await this.locks.run("books", async () => {
14837
+ const books = (await this.readBooks()).filter((entry) => entry.bookId !== book.bookId);
14838
+ books.push(book);
14839
+ await atomicJson(this.file("books.json"), { books: books.sort((left, right) => left.title.localeCompare(right.title)) }, 384);
14840
+ });
14136
14841
  }
14842
+ /**
14843
+ * Forget a book and its GitHub token. Its working copy is moved to the trash, never deleted: if that
14844
+ * fails nothing else is changed.
14845
+ */
14137
14846
  async removeBook(bookId) {
14138
- await atomicJson(this.file("books.json"), { books: (await this.books()).filter((entry) => entry.bookId !== bookId) }, 384);
14847
+ assertBookId(bookId);
14848
+ await this.moveToTrash(bookId);
14849
+ await this.locks.run("books", async () => {
14850
+ await atomicJson(this.file("books.json"), { books: (await this.readBooks()).filter((entry) => entry.bookId !== bookId) }, 384);
14851
+ });
14139
14852
  await this.setBookToken(bookId, null);
14140
- await fs5.rm(path5.join(this.root, "books", assertBookId(bookId)), { recursive: true, force: true });
14141
- }
14142
- async bookTokens() {
14143
- const stored = await readJson(this.file("private", "book-tokens.json"), 1024 * 1024);
14144
- const tokens = {};
14145
- for (const [bookId, value] of Object.entries(stored?.tokens ?? {})) if (BOOK_ID.test(bookId) && isEncryptedSecret(value)) tokens[bookId] = value;
14146
- return tokens;
14853
+ await fs5.rm(path6.join(this.root, "books", bookId), { recursive: true, force: true });
14147
14854
  }
14148
- async bookToken(bookId) {
14149
- const encrypted = (await this.bookTokens())[bookId];
14150
- return encrypted ? this.vault.decrypt(encrypted) : void 0;
14855
+ get trashDirectory() {
14856
+ return path6.join(this.root, "books", TRASH_DIRECTORY);
14151
14857
  }
14152
- async setBookToken(bookId, token) {
14153
- const tokens = await this.bookTokens();
14154
- if (token?.trim()) tokens[assertBookId(bookId)] = this.vault.encrypt(token.trim());
14155
- else delete tokens[bookId];
14156
- await atomicJson(this.file("private", "book-tokens.json"), { tokens });
14157
- }
14158
- };
14159
-
14160
- // src/fs-store.ts
14161
- import { createHash as createHash5 } from "node:crypto";
14162
- import { mkdir as mkdir5, readFile as readFile5, readdir as readdir2, rename as rename3, rm as rm4, stat, writeFile as writeFile2 } from "node:fs/promises";
14163
- import path6 from "node:path";
14164
- var SKIPPED_DIRECTORIES = /* @__PURE__ */ new Set([".git", "node_modules", ".astro", "dist"]);
14165
- var MAX_FILE_BYTES = 8 * 1024 * 1024;
14166
- function hashText(text3) {
14167
- return createHash5("sha256").update(text3, "utf8").digest("hex");
14168
- }
14169
- var KeyedMutex = class {
14170
- tails = /* @__PURE__ */ new Map();
14171
- run(key, action) {
14172
- const previous = this.tails.get(key) ?? Promise.resolve();
14173
- const next = previous.catch(() => void 0).then(action);
14174
- this.tails.set(key, next.finally(() => {
14175
- if (this.tails.get(key) === next) this.tails.delete(key);
14176
- }));
14177
- return next;
14178
- }
14179
- };
14180
- var FileBookStore = class {
14181
- constructor(root, options = {}) {
14182
- this.root = root;
14183
- this.options = options;
14184
- }
14185
- root;
14186
- options;
14187
- hashes = /* @__PURE__ */ new Map();
14188
- mutex = new KeyedMutex();
14189
- absolute(relative) {
14190
- const safe = assertSafeBookPath(relative);
14191
- const full = path6.resolve(this.root, ...safe.split("/"));
14192
- if (!full.startsWith(path6.resolve(this.root) + path6.sep)) throw new Error(`Unsafe book path: ${relative}`);
14193
- return full;
14194
- }
14195
- async hashOf(relative, full) {
14858
+ /**
14859
+ * Move a book's working copy to `books/.trash/<bookId>-<time>` (mode 0700) instead of deleting it.
14860
+ * Returns where it went, or `undefined` when there was nothing to keep.
14861
+ */
14862
+ async moveToTrash(bookId) {
14863
+ const directory = this.bookDirectory(bookId);
14864
+ let entries;
14196
14865
  try {
14197
- const info = await stat(full);
14198
- if (!info.isFile()) return null;
14199
- const cached = this.hashes.get(relative);
14200
- if (cached && cached.mtimeMs === info.mtimeMs && cached.size === info.size) return cached.hash;
14201
- const hash2 = hashText(await readFile5(full, "utf8"));
14202
- this.hashes.set(relative, { mtimeMs: info.mtimeMs, size: info.size, hash: hash2 });
14203
- return hash2;
14204
- } catch {
14205
- return null;
14866
+ entries = await fs5.readdir(directory);
14867
+ } catch (error) {
14868
+ if (error.code === "ENOENT") return void 0;
14869
+ throw error;
14206
14870
  }
14871
+ if (!entries.length) {
14872
+ await fs5.rmdir(directory);
14873
+ return void 0;
14874
+ }
14875
+ await fs5.mkdir(this.trashDirectory, { recursive: true, mode: 448 });
14876
+ let stamp = Date.now();
14877
+ while (await fs5.lstat(path6.join(this.trashDirectory, `${bookId}-${stamp}`)).then(() => true, () => false)) stamp += 1;
14878
+ const target2 = path6.join(this.trashDirectory, `${bookId}-${stamp}`);
14879
+ await renameReplacing(directory, target2);
14880
+ if (process.platform !== "win32") await fs5.chmod(target2, 448).catch(() => void 0);
14881
+ return target2;
14207
14882
  }
14208
- async list() {
14209
- const entries = [];
14210
- const walk = async (directory, prefix) => {
14211
- let children;
14212
- try {
14213
- children = await readdir2(directory, { withFileTypes: true });
14214
- } catch {
14215
- return;
14216
- }
14217
- for (const child of children) {
14218
- const relative = prefix ? `${prefix}/${child.name}` : child.name;
14219
- const full = path6.join(directory, child.name);
14220
- if (child.isDirectory()) {
14221
- if (!SKIPPED_DIRECTORIES.has(child.name)) await walk(full, relative);
14222
- } else if (child.isFile() && isTextBookPath(relative)) {
14223
- const hash2 = await this.hashOf(relative, full);
14224
- if (hash2) entries.push({ path: relative, hash: hash2 });
14225
- }
14226
- }
14227
- };
14228
- await walk(this.root, "");
14229
- return entries.sort((left, right) => left.path.localeCompare(right.path));
14230
- }
14231
- async read(relative) {
14232
- const full = this.absolute(relative);
14883
+ /**
14884
+ * Delete the trash entries older than `maxAgeMs`. The age is the time in the name (`<bookId>-<time>`):
14885
+ * a moved folder keeps its old modification time, so that time would be wrong for it. Entries without
14886
+ * a time in their name are judged by their modification time.
14887
+ */
14888
+ async purgeTrash(maxAgeMs = TRASH_RETENTION_MS, now = Date.now()) {
14889
+ let entries;
14233
14890
  try {
14234
- const info = await stat(full);
14235
- if (!info.isFile() || info.size > MAX_FILE_BYTES) return null;
14236
- const content = await readFile5(full, "utf8");
14237
- const hash2 = hashText(content);
14238
- this.hashes.set(relative, { mtimeMs: info.mtimeMs, size: info.size, hash: hash2 });
14239
- return { path: relative, content, hash: hash2 };
14240
- } catch {
14241
- return null;
14891
+ entries = await fs5.readdir(this.trashDirectory);
14892
+ } catch (error) {
14893
+ if (error.code === "ENOENT") return [];
14894
+ throw error;
14895
+ }
14896
+ const removed = [];
14897
+ for (const entry of entries) {
14898
+ const target2 = path6.join(this.trashDirectory, entry);
14899
+ const stamp = /^.+-(\d{10,16})$/.exec(entry)?.[1];
14900
+ const since = stamp ? Number(stamp) : await fs5.lstat(target2).then((info) => info.mtimeMs, () => now);
14901
+ if (now - since <= maxAgeMs) continue;
14902
+ await fs5.rm(target2, { recursive: true, force: true });
14903
+ removed.push(entry);
14242
14904
  }
14905
+ return removed;
14243
14906
  }
14244
- commit(changes, message) {
14245
- return this.mutex.run("write", async () => {
14246
- const prepared = changes.map((change) => ({ ...change, full: this.absolute(change.path) }));
14247
- for (const change of prepared) {
14248
- if (change.expectedHash === void 0) continue;
14249
- const current = await this.hashOf(change.path, change.full);
14250
- if (current !== change.expectedHash) throw new BookConflictError(`File changed since it was read: ${change.path}`, change.path);
14251
- }
14252
- const originals = /* @__PURE__ */ new Map();
14253
- for (const change of prepared) originals.set(change.full, await readFile5(change.full, "utf8").catch(() => null));
14254
- try {
14255
- for (const change of prepared) {
14256
- if (change.content === null) {
14257
- await rm4(change.full, { force: true });
14258
- } else {
14259
- if (Buffer.byteLength(change.content, "utf8") > MAX_FILE_BYTES) throw new Error(`File too large: ${change.path}`);
14260
- await mkdir5(path6.dirname(change.full), { recursive: true });
14261
- const temporary = `${change.full}.${process.pid}.${Date.now()}.tmp`;
14262
- await writeFile2(temporary, change.content, { encoding: "utf8", mode: 420 });
14263
- await rename3(temporary, change.full);
14264
- }
14265
- this.hashes.delete(change.path);
14266
- }
14267
- } catch (error) {
14268
- for (const [full, content] of originals) {
14269
- if (content === null) await rm4(full, { force: true }).catch(() => void 0);
14270
- else await writeFile2(full, content, "utf8").catch(() => void 0);
14271
- }
14272
- throw error;
14273
- }
14274
- const revision = await this.options.afterWrite?.(prepared.map((change) => change.path), message);
14275
- return { revision };
14907
+ async readBookTokens() {
14908
+ const stored = await readJson(this.file("private", "book-tokens.json"), 1024 * 1024);
14909
+ const tokens = {};
14910
+ for (const [bookId, value] of Object.entries(stored?.tokens ?? {})) if (BOOK_ID.test(bookId) && isEncryptedSecret(value)) tokens[bookId] = value;
14911
+ return tokens;
14912
+ }
14913
+ bookToken(bookId) {
14914
+ return this.locks.run("book-tokens", async () => {
14915
+ const encrypted = (await this.readBookTokens())[bookId];
14916
+ return encrypted ? this.vault.decrypt(encrypted) : void 0;
14276
14917
  });
14277
14918
  }
14278
- /** Revision of the current content when the store has no git history. */
14279
- async contentRevision() {
14280
- const entries = await this.list();
14281
- return hashText(entries.map((entry) => `${entry.path}:${entry.hash}`).join("\n")).slice(0, 16);
14919
+ async setBookToken(bookId, token) {
14920
+ assertBookToken(token);
14921
+ await this.locks.run("book-tokens", async () => {
14922
+ const tokens = await this.readBookTokens();
14923
+ if (token?.trim()) tokens[assertBookId(bookId)] = this.vault.encrypt(token.trim());
14924
+ else delete tokens[bookId];
14925
+ await atomicJson(this.file("private", "book-tokens.json"), { tokens });
14926
+ });
14282
14927
  }
14283
14928
  };
14284
14929
 
14285
14930
  // src/git.ts
14286
14931
  import { execFile } from "node:child_process";
14932
+ import { rmSync } from "node:fs";
14933
+ import { lstat as lstat6, mkdir as mkdir6, mkdtemp, writeFile as writeFile4 } from "node:fs/promises";
14934
+ import os2 from "node:os";
14935
+ import path7 from "node:path";
14287
14936
  var GitError = class extends Error {
14288
14937
  constructor(message, stderr = "", retryable = false) {
14289
14938
  super(message);
@@ -14297,15 +14946,46 @@ var GitError = class extends Error {
14297
14946
  };
14298
14947
  var TRANSIENT_GIT_FAILURE = /could not resolve host|temporary failure in name resolution|failed to connect|connection (?:timed out|refused|reset)|operation timed out|network is unreachable|early eof|remote end hung up|rpc failed|returned error: 5\d\d|internal server error|bad gateway|service unavailable|gnutls|ssl_connect|tls handshake/i;
14299
14948
  var AUTHOR = ["-c", "user.name=Narrarium Autopilot", "-c", "user.email=autopilot@narrarium.invalid"];
14300
- function gitEnvironment(token) {
14949
+ var MAX_ARGUMENT_CHARACTERS = 8e3;
14950
+ var sandboxes = /* @__PURE__ */ new Map();
14951
+ function prepareSandbox(home) {
14952
+ const key = path7.resolve(home);
14953
+ let ready = sandboxes.get(key);
14954
+ if (!ready) {
14955
+ ready = (async () => {
14956
+ await mkdir6(key, { recursive: true, mode: 448 });
14957
+ const hooks = path7.join(key, "hooks");
14958
+ await mkdir6(hooks, { recursive: true, mode: 448 });
14959
+ const config = path7.join(key, "gitconfig");
14960
+ await writeFile4(config, "", { mode: 384 });
14961
+ return { home: key, config, hooks };
14962
+ })();
14963
+ sandboxes.set(key, ready);
14964
+ ready.catch(() => sandboxes.delete(key));
14965
+ }
14966
+ return ready;
14967
+ }
14968
+ var temporaryHome;
14969
+ function defaultHome() {
14970
+ temporaryHome ??= mkdtemp(path7.join(os2.tmpdir(), "narrarium-git-")).then((directory) => {
14971
+ process.once("exit", () => rmSync(directory, { recursive: true, force: true }));
14972
+ return directory;
14973
+ });
14974
+ return temporaryHome;
14975
+ }
14976
+ function gitEnvironment(sandbox, token) {
14301
14977
  const env = {
14302
14978
  PATH: process.env.PATH,
14303
- HOME: process.env.HOME ?? process.env.USERPROFILE,
14304
- USERPROFILE: process.env.USERPROFILE,
14979
+ HOME: sandbox.home,
14980
+ USERPROFILE: sandbox.home,
14981
+ XDG_CONFIG_HOME: sandbox.home,
14305
14982
  SYSTEMROOT: process.env.SYSTEMROOT,
14306
14983
  LANG: "C",
14307
14984
  GIT_TERMINAL_PROMPT: "0",
14308
14985
  GIT_CONFIG_NOSYSTEM: "1",
14986
+ GIT_CONFIG_GLOBAL: sandbox.config,
14987
+ GIT_LITERAL_PATHSPECS: "1",
14988
+ GIT_LFS_SKIP_SMUDGE: "1",
14309
14989
  GIT_ASKPASS: "",
14310
14990
  SSH_ASKPASS: ""
14311
14991
  };
@@ -14317,56 +14997,111 @@ function gitEnvironment(token) {
14317
14997
  }
14318
14998
  return env;
14319
14999
  }
15000
+ function hardening(sandbox) {
15001
+ return [
15002
+ "-c",
15003
+ `core.hooksPath=${sandbox.hooks}`,
15004
+ "-c",
15005
+ "core.fsmonitor=false",
15006
+ "-c",
15007
+ "core.symlinks=false",
15008
+ "-c",
15009
+ "credential.helper=",
15010
+ "-c",
15011
+ "core.askPass=",
15012
+ "-c",
15013
+ "submodule.recurse=false",
15014
+ "-c",
15015
+ "protocol.ext.allow=never"
15016
+ ];
15017
+ }
14320
15018
  function redact(text3, token) {
14321
15019
  return token ? text3.split(token).join("***") : text3;
14322
15020
  }
14323
- function runGit(args2, options = {}) {
15021
+ function subcommand(args2) {
15022
+ for (let index = 0; index < args2.length; index += 1) {
15023
+ if (args2[index] === "-c") index += 1;
15024
+ else return args2[index];
15025
+ }
15026
+ return "";
15027
+ }
15028
+ function execGit(args2, env, options) {
14324
15029
  return new Promise((resolve, reject) => {
14325
- execFile("git", args2, { cwd: options.cwd, env: gitEnvironment(options.token), timeout: options.timeoutMs ?? 3e5, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (error, stdout, stderr) => {
15030
+ execFile("git", args2, { cwd: options.cwd, env, timeout: options.timeoutMs ?? 3e5, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (error, stdout, stderr) => {
14326
15031
  if (error) {
14327
15032
  const detail = redact(String(stderr || error.message).trim(), options.token);
14328
15033
  const timedOut = error.killed === true;
14329
- reject(new GitError(`git ${args2[0]} failed: ${detail.split("\n").slice(-3).join(" ")}`, detail, timedOut || TRANSIENT_GIT_FAILURE.test(detail)));
15034
+ reject(new GitError(`git ${subcommand(args2)} failed: ${detail.split("\n").slice(-3).join(" ")}`, detail, timedOut || TRANSIENT_GIT_FAILURE.test(detail)));
14330
15035
  return;
14331
15036
  }
14332
15037
  resolve({ stdout: String(stdout), stderr: String(stderr) });
14333
15038
  });
14334
15039
  });
14335
15040
  }
15041
+ async function runGit(args2, options = {}) {
15042
+ const sandbox = await prepareSandbox(options.home ?? await defaultHome());
15043
+ return execGit([...hardening(sandbox), ...args2], gitEnvironment(sandbox, options.token), options);
15044
+ }
14336
15045
  async function gitVersion() {
14337
15046
  try {
14338
- const { stdout } = await runGit(["--version"], { timeoutMs: 1e4 });
15047
+ const { stdout } = await execGit(["--version"], { PATH: process.env.PATH, SYSTEMROOT: process.env.SYSTEMROOT, LANG: "C" }, { timeoutMs: 1e4 });
14339
15048
  return stdout.trim();
14340
15049
  } catch {
14341
15050
  return null;
14342
15051
  }
14343
15052
  }
15053
+ function batches(paths, limit = MAX_ARGUMENT_CHARACTERS) {
15054
+ const groups = [];
15055
+ let current = [];
15056
+ let length = 0;
15057
+ for (const file of paths) {
15058
+ if (current.length && length + file.length + 1 > limit) {
15059
+ groups.push(current);
15060
+ current = [];
15061
+ length = 0;
15062
+ }
15063
+ current.push(file);
15064
+ length += file.length + 1;
15065
+ }
15066
+ if (current.length) groups.push(current);
15067
+ return groups;
15068
+ }
14344
15069
  var GitRepository = class {
14345
- constructor(dir, remote) {
15070
+ constructor(dir, remote, options = {}) {
14346
15071
  this.dir = dir;
14347
15072
  this.remote = remote;
15073
+ this.options = options;
14348
15074
  }
14349
15075
  dir;
14350
15076
  remote;
15077
+ options;
14351
15078
  get remoteUrl() {
14352
15079
  return this.remote ? this.remote.url ?? `https://github.com/${this.remote.owner}/${this.remote.repo}.git` : void 0;
14353
15080
  }
14354
- git(args2, timeoutMs) {
14355
- return runGit(args2, { cwd: this.dir, token: this.remote?.token(), timeoutMs });
15081
+ run(args2, options = {}) {
15082
+ return runGit(args2, { ...options, home: this.options.home });
15083
+ }
15084
+ /** Run in the working tree. The GitHub token goes only to the commands that talk to GitHub. */
15085
+ git(args2, network = false, timeoutMs) {
15086
+ return this.run(args2, { cwd: this.dir, token: network ? this.remote?.token() : void 0, timeoutMs });
14356
15087
  }
14357
15088
  async init() {
14358
- await runGit(["init", "--quiet", this.dir]);
15089
+ await this.run(["init", "--quiet", this.dir]);
15090
+ await this.git(["config", "--replace-all", "core.symlinks", "false"]);
14359
15091
  }
14360
15092
  /** Clone the book branch; when the branch does not exist yet it is created from the default branch. */
14361
15093
  async clone() {
14362
15094
  if (!this.remote) throw new GitError("This book has no GitHub repository.");
14363
15095
  const token = this.remote.token();
15096
+ const { branch } = this.remote;
15097
+ const settings = ["--quiet", "--config", "core.symlinks=false"];
14364
15098
  try {
14365
- await runGit(["clone", "--quiet", "--branch", this.remote.branch, "--single-branch", this.remoteUrl, this.dir], { token });
15099
+ await this.run(["clone", ...settings, "--branch", branch, "--single-branch", "--", this.remoteUrl, this.dir], { token });
14366
15100
  } catch {
14367
- await runGit(["clone", "--quiet", this.remoteUrl, this.dir], { token });
14368
- await this.git(["checkout", "--quiet", "-B", this.remote.branch]);
15101
+ await this.run(["clone", ...settings, "--", this.remoteUrl, this.dir], { token });
15102
+ await this.git(["checkout", "--quiet", "-B", branch]);
14369
15103
  }
15104
+ await this.git(["config", "--replace-all", "core.symlinks", "false"]);
14370
15105
  }
14371
15106
  async head() {
14372
15107
  try {
@@ -14375,19 +15110,32 @@ var GitRepository = class {
14375
15110
  return void 0;
14376
15111
  }
14377
15112
  }
15113
+ /** Stage and commit these paths (relative, `/`-separated, normalized by the caller). A failure leaves nothing staged. */
14378
15114
  async commit(paths, message) {
14379
- if (paths.length) await this.git(["add", "--all", "--", ...paths]);
14380
- const status = (await this.git(["status", "--porcelain"])).stdout.trim();
14381
- if (!status) return this.head();
14382
- await this.git([...AUTHOR, "commit", "--quiet", "--no-verify", "-m", message]);
15115
+ try {
15116
+ const present = [];
15117
+ const absent = [];
15118
+ for (const file of paths) (await lstat6(path7.join(this.dir, file)).then(() => true, () => false) ? present : absent).push(file);
15119
+ for (const batch of batches(present)) await this.git(["add", "--all", "--", ...batch]);
15120
+ for (const batch of batches(absent)) await this.git(["rm", "--cached", "--quiet", "--ignore-unmatch", "--", ...batch]);
15121
+ if (!(await this.git(["diff", "--cached", "--name-only"])).stdout.trim()) return this.head();
15122
+ await this.git([...AUTHOR, "commit", "--quiet", "--no-verify", "-m", message]);
15123
+ } catch (error) {
15124
+ await this.unstage(paths);
15125
+ throw error;
15126
+ }
14383
15127
  return this.head();
14384
15128
  }
15129
+ /** After a failed commit the index goes back to HEAD for these paths, so the next commit does not carry them. */
15130
+ async unstage(paths) {
15131
+ for (const batch of batches(paths)) await this.git(["reset", "--quiet", "--", ...batch]).catch(() => void 0);
15132
+ }
14385
15133
  /** Bring in the author's changes: fast-forward when possible, otherwise rebase local commits. */
14386
15134
  async pull() {
14387
15135
  if (!this.remote) return { changed: false };
14388
15136
  const before = await this.head();
14389
15137
  try {
14390
- await this.git(["fetch", "--quiet", "origin", this.remote.branch]);
15138
+ await this.git(["fetch", "--quiet", "--", "origin", this.remote.branch], true);
14391
15139
  } catch (error) {
14392
15140
  if (error instanceof GitError && /couldn't find remote ref/i.test(error.stderr)) return { changed: false };
14393
15141
  throw error;
@@ -14407,16 +15155,17 @@ var GitRepository = class {
14407
15155
  async push() {
14408
15156
  if (!this.remote) return;
14409
15157
  try {
14410
- await this.git(["push", "--quiet", "origin", `HEAD:refs/heads/${this.remote.branch}`]);
15158
+ await this.git(["push", "--quiet", "origin", `HEAD:refs/heads/${this.remote.branch}`], true);
14411
15159
  } catch (error) {
14412
15160
  if (!(error instanceof GitError) || !/rejected|fetch first|non-fast-forward/i.test(error.stderr)) throw error;
14413
15161
  await this.pull();
14414
- await this.git(["push", "--quiet", "origin", `HEAD:refs/heads/${this.remote.branch}`]);
15162
+ await this.git(["push", "--quiet", "origin", `HEAD:refs/heads/${this.remote.branch}`], true);
14415
15163
  }
14416
15164
  }
14417
15165
  };
14418
15166
 
14419
15167
  // src/books.ts
15168
+ var MAX_BOOKS = 200;
14420
15169
  function sameStorage(left, right) {
14421
15170
  return JSON.stringify(left) === JSON.stringify(right);
14422
15171
  }
@@ -14424,7 +15173,7 @@ function validStorage(storage) {
14424
15173
  const value = storage;
14425
15174
  if (value?.kind === "local") return { kind: "local" };
14426
15175
  const segment = /^[A-Za-z0-9._-]{1,100}$/;
14427
- const branch = /^(?!.*\.\.)(?!\/)[A-Za-z0-9._/@+-]{1,200}$/;
15176
+ const branch = /^(?!-)(?!.*\.\.)(?!\/)[A-Za-z0-9._/@+-]{1,200}$/;
14428
15177
  if (value?.kind === "github" && typeof value.owner === "string" && segment.test(value.owner) && typeof value.repo === "string" && segment.test(value.repo) && typeof value.branch === "string" && branch.test(value.branch)) {
14429
15178
  return { kind: "github", owner: value.owner, repo: value.repo, branch: value.branch };
14430
15179
  }
@@ -14438,6 +15187,10 @@ var BookRegistry = class {
14438
15187
  data;
14439
15188
  options;
14440
15189
  runtimes = /* @__PURE__ */ new Map();
15190
+ /** Ids of new books being registered right now: they already take a place. */
15191
+ arriving = /* @__PURE__ */ new Set();
15192
+ /** `register` and `remove` of one book never overlap. */
15193
+ locks = new KeyedMutex();
14441
15194
  aiCache = null;
14442
15195
  /** Set once an update is accepted: the server only finishes what it is doing and then restarts. */
14443
15196
  maintenance = false;
@@ -14478,6 +15231,12 @@ var BookRegistry = class {
14478
15231
  }
14479
15232
  };
14480
15233
  async load() {
15234
+ await this.data.purgeTrash().then(
15235
+ (removed) => {
15236
+ if (removed.length) this.options.log(`Deleted ${removed.length} book copies from the trash after the retention period.`);
15237
+ },
15238
+ (error) => this.options.log(`The trash could not be cleaned: ${error instanceof Error ? error.message : String(error)}`)
15239
+ );
14481
15240
  for (const book of await this.data.books()) {
14482
15241
  try {
14483
15242
  this.instantiate(book);
@@ -14486,12 +15245,22 @@ var BookRegistry = class {
14486
15245
  }
14487
15246
  }
14488
15247
  }
15248
+ /** The git side of a working copy; `null` when git is not installed. Git runs in a private home inside the data folder. */
15249
+ makeGit(directory, storage, token) {
15250
+ if (!this.options.gitAvailable) return null;
15251
+ const remote = storage.kind === "github" ? { owner: storage.owner, repo: storage.repo, branch: storage.branch, token, url: this.options.githubRemoteUrl?.(storage.owner, storage.repo) } : void 0;
15252
+ return new GitRepository(directory, remote, { home: this.data.file("private", "git-home") });
15253
+ }
14489
15254
  instantiate(book) {
14490
- this.runtimes.get(book.bookId)?.unsubscribe();
15255
+ const previous = this.runtimes.get(book.bookId);
15256
+ if (previous) {
15257
+ previous.session.pause("now");
15258
+ previous.unsubscribe();
15259
+ }
14491
15260
  const directory = this.data.bookDirectory(book.bookId);
14492
- const git = this.options.gitAvailable ? new GitRepository(directory, book.storage.kind === "github" ? { owner: book.storage.owner, repo: book.storage.repo, branch: book.storage.branch, token: () => this.tokenCache.get(book.bookId), url: this.options.githubRemoteUrl?.(book.storage.owner, book.storage.repo) } : void 0) : null;
15261
+ const git = this.makeGit(directory, book.storage, () => this.tokenCache.get(book.bookId));
14493
15262
  const store = new FileBookStore(directory, {
14494
- afterWrite: async (paths, message) => git && existsSync2(path7.join(directory, ".git")) ? git.commit(paths, message) : void 0
15263
+ afterWrite: async (paths, message) => git && existsSync2(path8.join(directory, ".git")) ? git.commit(paths, message) : void 0
14495
15264
  });
14496
15265
  const runtime = {
14497
15266
  book,
@@ -14554,38 +15323,41 @@ var BookRegistry = class {
14554
15323
  }
14555
15324
  async register(bookId, request2) {
14556
15325
  assertBookId(bookId);
15326
+ this.assertNotInMaintenance();
15327
+ assertBookToken(request2.githubToken);
15328
+ const isNew = !this.runtimes.has(bookId) && !this.arriving.has(bookId);
15329
+ if (isNew) {
15330
+ if (this.runtimes.size + this.arriving.size >= MAX_BOOKS) throw new HttpError(409, "too_many_books", `This server hosts at most ${MAX_BOOKS} books: remove one before adding another.`);
15331
+ this.arriving.add(bookId);
15332
+ }
15333
+ try {
15334
+ return await this.locks.run(bookId, () => this.registerExclusive(bookId, request2));
15335
+ } finally {
15336
+ if (isNew) this.arriving.delete(bookId);
15337
+ }
15338
+ }
15339
+ async registerExclusive(bookId, request2) {
14557
15340
  this.assertNotInMaintenance();
14558
15341
  const title = typeof request2.title === "string" && request2.title.trim() ? request2.title.trim().slice(0, 300) : bookId;
14559
15342
  const storage = validStorage(request2.storage);
14560
15343
  const existing = this.runtimes.get(bookId);
14561
- if (existing?.session.running && !sameStorage(existing.book.storage, storage)) throw new HttpError(409, "busy", "Pause the autopilot before changing where the book is stored.");
14562
- if (request2.githubToken !== void 0) await this.data.setBookToken(bookId, request2.githubToken);
14563
- const directory = this.data.bookDirectory(bookId);
14564
- const now = (/* @__PURE__ */ new Date()).toISOString();
14565
- const book = { bookId, title, storage, createdAt: existing?.book.createdAt ?? now, updatedAt: now };
14566
- const moved = existing && !sameStorage(existing.book.storage, storage);
14567
- if (moved) await rm5(directory, { recursive: true, force: true });
14568
- await mkdir6(path7.dirname(directory), { recursive: true, mode: 448 });
15344
+ const unchanged = existing !== void 0 && sameStorage(existing.book.storage, storage);
15345
+ if (existing?.session.running && !unchanged) throw new HttpError(409, "busy", "Pause the autopilot before changing where the book is stored.");
15346
+ const token = request2.githubToken === void 0 ? await this.data.bookToken(bookId) : request2.githubToken?.trim() || void 0;
14569
15347
  if (storage.kind === "github") {
14570
15348
  if (!this.options.gitAvailable) throw new HttpError(412, "git_unavailable", "Install git on the server to host GitHub books.");
14571
- if (!await this.data.bookToken(bookId)) throw new HttpError(412, "github_token_required", "Send the book's GitHub token (with your consent) so the server can clone and push it.");
15349
+ if (!token) throw new HttpError(412, "github_token_required", "Send the book's GitHub token (with your consent) so the server can clone and push it.");
14572
15350
  }
15351
+ const now = (/* @__PURE__ */ new Date()).toISOString();
15352
+ const book = { bookId, title, storage, createdAt: existing?.book.createdAt ?? now, updatedAt: now };
15353
+ if (existing && !unchanged) return this.move(existing, book, request2, token);
15354
+ if (request2.githubToken !== void 0) await this.data.setBookToken(bookId, request2.githubToken);
14573
15355
  await this.data.saveBook(book);
14574
- const runtime = this.instantiate(book);
15356
+ const runtime = existing ?? this.instantiate(book);
15357
+ runtime.book = book;
14575
15358
  try {
14576
- if (storage.kind === "github" && runtime.git) {
14577
- await this.refreshToken(bookId);
14578
- if (!existsSync2(path7.join(directory, ".git"))) {
14579
- await rm5(directory, { recursive: true, force: true });
14580
- await runtime.git.clone();
14581
- runtime.lastSync = { at: (/* @__PURE__ */ new Date()).toISOString(), ok: true, message: "Cloned from GitHub.", head: await runtime.git.head() };
14582
- } else if (!runtime.session.running) {
14583
- await this.syncGitHub(bookId);
14584
- }
14585
- } else {
14586
- await mkdir6(directory, { recursive: true });
14587
- if (runtime.git && !existsSync2(path7.join(directory, ".git"))) await runtime.git.init();
14588
- }
15359
+ if (storage.kind === "github") await this.refreshToken(bookId);
15360
+ if (!runtime.session.running) await this.prepareRepository(runtime);
14589
15361
  runtime.error = void 0;
14590
15362
  } catch (error) {
14591
15363
  runtime.error = error instanceof Error ? error.message : String(error);
@@ -14595,12 +15367,91 @@ var BookRegistry = class {
14595
15367
  }
14596
15368
  return this.summary(runtime);
14597
15369
  }
15370
+ /** Make sure the working copy exists and, for a GitHub book, is up to date. */
15371
+ async prepareRepository(runtime) {
15372
+ const { book, git } = runtime;
15373
+ const directory = this.data.bookDirectory(book.bookId);
15374
+ if (book.storage.kind === "github" && git) {
15375
+ if (existsSync2(path8.join(directory, ".git"))) {
15376
+ await this.syncGitHub(book.bookId);
15377
+ return;
15378
+ }
15379
+ const staged = await this.stage(book.bookId, book.storage, () => this.tokenCache.get(book.bookId));
15380
+ await this.swapIn(book.bookId, staged.directory);
15381
+ runtime.lastSync = { at: (/* @__PURE__ */ new Date()).toISOString(), ok: true, message: "Cloned from GitHub.", head: staged.head };
15382
+ } else {
15383
+ await mkdir7(directory, { recursive: true });
15384
+ if (git && !existsSync2(path8.join(directory, ".git"))) await git.init();
15385
+ }
15386
+ }
15387
+ /**
15388
+ * Build a working copy next to the final one (a clone, or an empty repository), so the copy in place
15389
+ * stays untouched until the new one is complete.
15390
+ */
15391
+ async stage(bookId, storage, token) {
15392
+ const final = this.data.bookDirectory(bookId);
15393
+ const parent = path8.dirname(final);
15394
+ await mkdir7(parent, { recursive: true, mode: 448 });
15395
+ for (const entry of await readdir4(parent)) if (entry.startsWith("repo.new-")) await rm5(path8.join(parent, entry), { recursive: true, force: true });
15396
+ const directory = `${final}.new-${randomUUID6().slice(0, 8)}`;
15397
+ try {
15398
+ const git = this.makeGit(directory, storage, token);
15399
+ if (storage.kind === "github") {
15400
+ await git?.clone();
15401
+ } else {
15402
+ await mkdir7(directory, { recursive: true });
15403
+ await git?.init();
15404
+ }
15405
+ return { directory, head: await git?.head() };
15406
+ } catch (error) {
15407
+ await rm5(directory, { recursive: true, force: true }).catch(() => void 0);
15408
+ throw error;
15409
+ }
15410
+ }
15411
+ /** Put a staged working copy in place; the copy it replaces goes to the trash, never to waste. */
15412
+ async swapIn(bookId, staged) {
15413
+ const directory = this.data.bookDirectory(bookId);
15414
+ const trashed = await this.data.moveToTrash(bookId);
15415
+ try {
15416
+ await renameReplacing(staged, directory);
15417
+ } catch (error) {
15418
+ if (trashed) await renameReplacing(trashed, directory).catch(() => void 0);
15419
+ await rm5(staged, { recursive: true, force: true }).catch(() => void 0);
15420
+ throw error;
15421
+ }
15422
+ }
15423
+ /** Change where a book is stored: the new copy is built first, and a failure leaves the old copy and the registration as they were. */
15424
+ async move(existing, book, request2, token) {
15425
+ const { bookId } = book;
15426
+ let staged;
15427
+ try {
15428
+ staged = await this.stage(bookId, book.storage, () => token);
15429
+ } catch (error) {
15430
+ throw new HttpError(502, "repository_unavailable", error instanceof Error ? error.message : String(error));
15431
+ }
15432
+ if (existing.session.running) {
15433
+ await rm5(staged.directory, { recursive: true, force: true }).catch(() => void 0);
15434
+ throw new HttpError(409, "busy", "Pause the autopilot before changing where the book is stored.");
15435
+ }
15436
+ await this.swapIn(bookId, staged.directory);
15437
+ if (request2.githubToken !== void 0) await this.data.setBookToken(bookId, request2.githubToken);
15438
+ await this.data.saveBook(book);
15439
+ const runtime = this.instantiate(book);
15440
+ if (book.storage.kind === "github") await this.refreshToken(bookId);
15441
+ runtime.lastSync = book.storage.kind === "github" ? { at: (/* @__PURE__ */ new Date()).toISOString(), ok: true, message: "Cloned from GitHub.", head: staged.head } : null;
15442
+ this.options.onBookChanged(bookId);
15443
+ return this.summary(runtime);
15444
+ }
14598
15445
  async remove(bookId) {
14599
- const runtime = this.get(bookId);
14600
- if (runtime.session.running) throw new HttpError(409, "busy", "Pause the autopilot before removing the book from the server.");
14601
- runtime.unsubscribe();
14602
- this.runtimes.delete(bookId);
14603
- await this.data.removeBook(bookId);
15446
+ assertBookId(bookId);
15447
+ return this.locks.run(bookId, async () => {
15448
+ const runtime = this.get(bookId);
15449
+ if (runtime.session.running) throw new HttpError(409, "busy", "Pause the autopilot before removing the book from the server.");
15450
+ await this.data.removeBook(bookId);
15451
+ runtime.unsubscribe();
15452
+ this.runtimes.delete(bookId);
15453
+ this.tokenCache.delete(bookId);
15454
+ });
14604
15455
  }
14605
15456
  async syncGitHub(bookId) {
14606
15457
  const runtime = this.get(bookId);
@@ -14620,7 +15471,8 @@ var BookRegistry = class {
14620
15471
  }
14621
15472
  async summary(runtime) {
14622
15473
  const snapshot = await runtime.session.snapshot();
14623
- const revision = (runtime.git && existsSync2(path7.join(runtime.store.root, ".git")) ? await runtime.git.head() : void 0) ?? await runtime.store.contentRevision();
15474
+ const revision = (runtime.git && existsSync2(path8.join(runtime.store.root, ".git")) ? await runtime.git.head() : void 0) ?? await runtime.store.contentRevision();
15475
+ const error = runtime.error ?? snapshot.stateError?.message;
14624
15476
  return {
14625
15477
  bookId: runtime.book.bookId,
14626
15478
  title: runtime.book.title,
@@ -14631,7 +15483,7 @@ var BookRegistry = class {
14631
15483
  autopilot: snapshot.state ? summarizeAutopilotState(snapshot.state) : null,
14632
15484
  running: runtime.session.running,
14633
15485
  lastSync: runtime.lastSync,
14634
- ...runtime.error ? { error: runtime.error } : {}
15486
+ ...error ? { error } : {}
14635
15487
  };
14636
15488
  }
14637
15489
  async summaries() {
@@ -14647,12 +15499,63 @@ var BookRegistry = class {
14647
15499
  }
14648
15500
  };
14649
15501
 
15502
+ // src/gateway.ts
15503
+ import { request as httpRequest2 } from "node:http";
15504
+ import { pipeline } from "node:stream";
15505
+ var HOP_BY_HOP = /* @__PURE__ */ new Set(["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "proxy-connection", "te", "trailer", "transfer-encoding", "upgrade", "expect"]);
15506
+ var ALWAYS_FORWARDED = /* @__PURE__ */ new Set(["authorization", "cf-connecting-ip"]);
15507
+ function forwardedHeaders(headers) {
15508
+ const dropped = new Set(HOP_BY_HOP);
15509
+ for (const entry of String(headers.connection ?? "").split(",")) {
15510
+ const name = entry.trim().toLowerCase();
15511
+ if (name) dropped.add(name);
15512
+ }
15513
+ const forwarded = {};
15514
+ for (const [name, value] of Object.entries(headers)) {
15515
+ if (value === void 0) continue;
15516
+ if (!ALWAYS_FORWARDED.has(name) && (dropped.has(name) || name.startsWith("proxy-"))) continue;
15517
+ forwarded[name] = value;
15518
+ }
15519
+ return forwarded;
15520
+ }
15521
+ function bridgeToApi(request2, response, target2, agent) {
15522
+ const upstream = httpRequest2({
15523
+ hostname: "127.0.0.1",
15524
+ port: target2.port,
15525
+ path: request2.url ?? "/",
15526
+ method: request2.method,
15527
+ agent,
15528
+ headers: { ...forwardedHeaders(request2.headers), host: target2.host, "x-narrarium-proxy": "admin-gateway" }
15529
+ }, (upstreamResponse) => {
15530
+ if (response.destroyed) {
15531
+ upstreamResponse.destroy();
15532
+ return;
15533
+ }
15534
+ response.writeHead(upstreamResponse.statusCode ?? 502, forwardedHeaders(upstreamResponse.headers));
15535
+ response.flushHeaders();
15536
+ pipeline(upstreamResponse, response, () => upstream.destroy());
15537
+ });
15538
+ upstream.on("error", () => {
15539
+ if (response.writableEnded || response.destroyed) return;
15540
+ if (response.headersSent) {
15541
+ response.destroy();
15542
+ return;
15543
+ }
15544
+ const body = JSON.stringify({ code: "api_proxy", message: "The server API is not reachable." });
15545
+ response.writeHead(503, { "content-type": "application/json; charset=utf-8", "content-length": Buffer.byteLength(body), connection: "close" });
15546
+ response.end(body);
15547
+ });
15548
+ upstream.setTimeout(target2.idleMs ?? 13e4, () => upstream.destroy(new Error("timeout")));
15549
+ response.once("close", () => upstream.destroy());
15550
+ request2.pipe(upstream);
15551
+ }
15552
+
14650
15553
  // src/tunnel.ts
14651
15554
  import { spawn as spawn4 } from "node:child_process";
14652
- import { randomUUID as randomUUID5 } from "node:crypto";
14653
- import { constants as constants3 } from "node:fs";
15555
+ import { randomUUID as randomUUID7 } from "node:crypto";
15556
+ import { constants as constants4 } from "node:fs";
14654
15557
  import * as fs6 from "node:fs/promises";
14655
- import path8 from "node:path";
15558
+ import path9 from "node:path";
14656
15559
  var STATE_FILE = "cloudflare-tunnel.json";
14657
15560
  var TOKEN_FILE = "cloudflare-tunnel-token";
14658
15561
  var MIN_VERSION = [2025, 4, 0];
@@ -14728,7 +15631,7 @@ function buildCloudflaredEnvironment(root) {
14728
15631
  }
14729
15632
  const noProxy = [process.env.NO_PROXY, process.env.no_proxy].filter(Boolean).flatMap((value) => value.split(",")).map((value) => value.trim()).filter(Boolean);
14730
15633
  for (const value of ["127.0.0.1", "localhost"]) if (!noProxy.includes(value)) noProxy.push(value);
14731
- const home = path8.join(root, "private", "cloudflared-home");
15634
+ const home = path9.join(root, "private", "cloudflared-home");
14732
15635
  Object.assign(env, {
14733
15636
  HOME: home,
14734
15637
  XDG_CONFIG_HOME: home,
@@ -14785,7 +15688,7 @@ async function detectSystemCloudflaredPids() {
14785
15688
  const pid = Number(entry);
14786
15689
  if (pid === process.pid || !Number.isSafeInteger(pid)) return;
14787
15690
  try {
14788
- const command = (await fs6.readFile(path8.join("/proc", entry, "comm"), "utf8")).trim().toLowerCase();
15691
+ const command = (await fs6.readFile(path9.join("/proc", entry, "comm"), "utf8")).trim().toLowerCase();
14789
15692
  if (command === "cloudflared") pids2.push(pid);
14790
15693
  } catch {
14791
15694
  }
@@ -14801,9 +15704,9 @@ async function detectSystemCloudflaredPids() {
14801
15704
  for (const line of output.split("\n")) {
14802
15705
  const match = /^\s*(\d+)\s+(.+?)\s+(.+)$/.exec(line);
14803
15706
  if (!match || Number(match[1]) === process.pid) continue;
14804
- const command = path8.basename(match[2].trim()).toLowerCase();
15707
+ const command = path9.basename(match[2].trim()).toLowerCase();
14805
15708
  const firstArgument = match[3].trim().split(/\s+/)[0] ?? "";
14806
- if (command === "cloudflared" || path8.basename(firstArgument).toLowerCase() === "cloudflared") pids.push(Number(match[1]));
15709
+ if (command === "cloudflared" || path9.basename(firstArgument).toLowerCase() === "cloudflared") pids.push(Number(match[1]));
14807
15710
  }
14808
15711
  return pids;
14809
15712
  }
@@ -14822,9 +15725,9 @@ var CloudflareTunnelManager = class _CloudflareTunnelManager {
14822
15725
  current = { enabled: false, configured: false, process: "stopped", binaryAvailable: false, otherConnectorDetected: false };
14823
15726
  constructor(options) {
14824
15727
  this.root = options.root;
14825
- this.privateDir = path8.join(this.root, "private");
14826
- this.statePath = path8.join(this.privateDir, STATE_FILE);
14827
- this.tokenPath = path8.join(this.privateDir, TOKEN_FILE);
15728
+ this.privateDir = path9.join(this.root, "private");
15729
+ this.statePath = path9.join(this.privateDir, STATE_FILE);
15730
+ this.tokenPath = path9.join(this.privateDir, TOKEN_FILE);
14828
15731
  this.findBinary = options.findBinary ?? (() => process.platform === "win32" ? "cloudflared.exe" : "cloudflared");
14829
15732
  this.spawn = options.spawn ?? ((command, args2, spawnOptions) => spawn4(command, [...args2], spawnOptions));
14830
15733
  this.detectOtherConnectors = options.detectOtherConnectors ?? (async () => detectSystemCloudflaredPids());
@@ -14865,7 +15768,7 @@ var CloudflareTunnelManager = class _CloudflareTunnelManager {
14865
15768
  const info = await fs6.lstat(this.tokenPath);
14866
15769
  if (!info.isFile() || info.isSymbolicLink() || info.nlink !== 1 || info.size < 1 || info.size > 4 * MAX_TOKEN_CHARS) throw new Error("invalid token");
14867
15770
  if (process.platform !== "win32" && (info.mode & 63) !== 0) throw new Error("invalid token mode");
14868
- const handle = await fs6.open(this.tokenPath, constants3.O_RDONLY | (constants3.O_NOFOLLOW ?? 0));
15771
+ const handle = await fs6.open(this.tokenPath, constants4.O_RDONLY | (constants4.O_NOFOLLOW ?? 0));
14869
15772
  try {
14870
15773
  const opened = await handle.stat();
14871
15774
  if (opened.dev !== info.dev || opened.ino !== info.ino || opened.size !== info.size) throw new Error("changed token");
@@ -14951,7 +15854,7 @@ var CloudflareTunnelManager = class _CloudflareTunnelManager {
14951
15854
  this.setError("version");
14952
15855
  return this.status();
14953
15856
  }
14954
- await fs6.mkdir(path8.join(this.privateDir, "cloudflared-home"), { recursive: true, mode: 448 });
15857
+ await fs6.mkdir(path9.join(this.privateDir, "cloudflared-home"), { recursive: true, mode: 448 });
14955
15858
  let child;
14956
15859
  try {
14957
15860
  child = this.spawn(binary, ["tunnel", "run", "--no-autoupdate", "--token-file", this.tokenPath], { cwd: this.root, env: buildCloudflaredEnvironment(this.root), shell: false, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
@@ -15075,7 +15978,7 @@ var CloudflareTunnelManager = class _CloudflareTunnelManager {
15075
15978
  } catch (error) {
15076
15979
  if (error.code !== "ENOENT") throw error;
15077
15980
  }
15078
- const temporary = `${this.tokenPath}.new-${randomUUID5()}`;
15981
+ const temporary = `${this.tokenPath}.new-${randomUUID7()}`;
15079
15982
  const handle = await fs6.open(temporary, "wx", 384);
15080
15983
  try {
15081
15984
  await handle.writeFile(token, "utf8");
@@ -15564,13 +16467,25 @@ var ADMIN_JS = String.raw`"use strict";
15564
16467
  `;
15565
16468
 
15566
16469
  // src/version.ts
15567
- var SERVER_VERSION = "0.1.1";
16470
+ var SERVER_VERSION = "0.1.2";
15568
16471
 
15569
16472
  // src/server.ts
15570
16473
  var SMALL_BODY = 2 * 1024 * 1024;
15571
16474
  var LARGE_BODY = 64 * 1024 * 1024;
15572
- var MAX_STREAMS = 20;
15573
- function readBody2(request2, limit) {
16475
+ var MAX_STREAMS = 50;
16476
+ var MAX_STREAMS_PER_CLIENT = 10;
16477
+ var MAX_LARGE_BODIES = 2;
16478
+ var ANONYMOUS_IN_FLIGHT = 16;
16479
+ var BODY_IDLE_MS = 2e4;
16480
+ var REQUEST_TIMEOUT_MS = 3e5;
16481
+ var MINUTE_MS = 6e4;
16482
+ var INFO_PER_MINUTE = 120;
16483
+ var PAIRING_RESULTS_PER_MINUTE = 120;
16484
+ var FAILED_AUTHENTICATIONS_PER_MINUTE = 60;
16485
+ var AUTHENTICATED_PER_MINUTE = 6e3;
16486
+ var RELEASE_CACHE_MS = 10 * MINUTE_MS;
16487
+ var RELEASE_UNREACHABLE_CACHE_MS = MINUTE_MS;
16488
+ function readJsonBody(request2, limit, idleMs) {
15574
16489
  return new Promise((resolve, reject) => {
15575
16490
  const type = String(request2.headers["content-type"] ?? "");
15576
16491
  if (type && !/^application\/json\b/i.test(type)) {
@@ -15589,16 +16504,29 @@ function readBody2(request2, limit) {
15589
16504
  }
15590
16505
  const chunks = [];
15591
16506
  let size = 0;
15592
- request2.on("data", (chunk) => {
16507
+ let quiet;
16508
+ const stop = (finish) => {
16509
+ clearTimeout(quiet);
16510
+ request2.off("data", onData);
16511
+ request2.off("end", onEnd);
16512
+ request2.off("error", onAbort);
16513
+ request2.off("close", onAbort);
16514
+ finish();
16515
+ };
16516
+ const waitForData = () => {
16517
+ clearTimeout(quiet);
16518
+ quiet = setTimeout(() => stop(() => reject(new HttpError(408, "request_timeout", "The request body stopped arriving."))), idleMs);
16519
+ };
16520
+ const onData = (chunk) => {
15593
16521
  size += chunk.length;
15594
16522
  if (size > limit) {
15595
- reject(new HttpError(413, "payload_too_large", "The request is too large."));
15596
- request2.destroy();
16523
+ stop(() => reject(new HttpError(413, "payload_too_large", "The request is too large.")));
15597
16524
  return;
15598
16525
  }
15599
16526
  chunks.push(chunk);
15600
- });
15601
- request2.on("end", () => {
16527
+ waitForData();
16528
+ };
16529
+ const onEnd = () => stop(() => {
15602
16530
  if (!chunks.length) {
15603
16531
  resolve({});
15604
16532
  return;
@@ -15611,22 +16539,55 @@ function readBody2(request2, limit) {
15611
16539
  reject(new HttpError(400, "invalid_json", "The request body is not valid JSON."));
15612
16540
  }
15613
16541
  });
15614
- request2.on("error", reject);
16542
+ const onAbort = () => stop(() => reject(new HttpError(400, "request_aborted", "The request was interrupted before its body arrived.")));
16543
+ request2.on("data", onData);
16544
+ request2.once("end", onEnd);
16545
+ request2.once("error", onAbort);
16546
+ request2.once("close", onAbort);
16547
+ waitForData();
15615
16548
  });
15616
16549
  }
15617
- function sendJson(response, status, payload) {
16550
+ function sendJson(response, status, payload, headers = {}) {
15618
16551
  const body = JSON.stringify(payload);
15619
- response.writeHead(status, { "content-type": "application/json; charset=utf-8", "content-length": Buffer.byteLength(body) });
16552
+ response.writeHead(status, { "content-type": "application/json; charset=utf-8", "content-length": Buffer.byteLength(body), ...headers });
15620
16553
  response.end(body);
15621
16554
  }
16555
+ function describeUnknown(error) {
16556
+ if (error instanceof Error) {
16557
+ const frame = (error.stack ?? "").split("\n").find((line) => /^\s+at /.test(line))?.trim();
16558
+ return `${error.name}: ${error.message.slice(0, 500)}${frame ? ` (${frame})` : ""}`;
16559
+ }
16560
+ let text3;
16561
+ try {
16562
+ text3 = typeof error === "object" && error !== null ? JSON.stringify(error) : String(error);
16563
+ } catch {
16564
+ text3 = String(error);
16565
+ }
16566
+ return `Not an Error: ${text3.slice(0, 500)}`;
16567
+ }
15622
16568
  function errorStatus(error) {
15623
- if (error instanceof HttpError) return { status: error.status, code: error.code, message: error.message };
16569
+ if (error instanceof HttpError) return { status: error.status, code: error.code, message: error.message, headers: error.headers };
15624
16570
  if (error instanceof AiUnavailableError) return { status: 412, code: "ai_not_configured", message: error.message };
15625
16571
  if (error instanceof AutopilotBusyError) return { status: 409, code: "busy", message: error.message };
15626
16572
  if (isBookConflictError(error)) return { status: 409, code: "conflict", message: error.message };
15627
- const status = typeof error?.status === "number" ? error.status : 500;
15628
- const code = typeof error?.code === "string" ? error.code : "internal";
15629
- return { status, code, message: error instanceof Error ? error.message : String(error) };
16573
+ if (error instanceof AutopilotStateCorruptError) return { status: 409, code: "state_corrupt", message: error.message };
16574
+ if (error instanceof AiProviderError) return { status: 502, code: "ai_provider", message: error.message };
16575
+ if (error instanceof Error && error.message.startsWith("Unsafe book path")) return { status: 400, code: "invalid_path", message: error.message };
16576
+ return { status: 500, code: "internal", message: "The server could not complete the request.", requestId: randomBytes4(4).toString("hex"), detail: describeUnknown(error) };
16577
+ }
16578
+ function isFailure(mapped) {
16579
+ return mapped.status >= 500 && mapped.status !== 503;
16580
+ }
16581
+ function sendError(response, mapped) {
16582
+ sendJson(
16583
+ response,
16584
+ mapped.status,
16585
+ { code: mapped.code, message: mapped.message, ...mapped.requestId ? { requestId: mapped.requestId } : {} },
16586
+ { ...mapped.headers, ...mapped.requestId ? { "x-request-id": mapped.requestId } : {} }
16587
+ );
16588
+ }
16589
+ function refuseOver(verdict) {
16590
+ if (!verdict.allowed) throw new HttpError(429, "rate_limit", "Too many requests: wait a moment and try again.", retryAfter(verdict.retryAfterMs));
15630
16591
  }
15631
16592
  function asObject(value) {
15632
16593
  if (!value || typeof value !== "object" || Array.isArray(value)) throw new HttpError(400, "invalid_request", "Expected a JSON object.");
@@ -15687,6 +16648,19 @@ async function startNarrariumServer(options) {
15687
16648
  const checkRelease = options.checkRelease ?? ((version) => checkNpmServerRelease(version));
15688
16649
  const installer = new RuntimeInstaller({ dataRoot: data2.root });
15689
16650
  const gitAvailable = Boolean(await gitVersion());
16651
+ const bodyIdleMs = typeof options.limits?.bodyIdleMs === "number" && options.limits.bodyIdleMs > 0 ? options.limits.bodyIdleMs : BODY_IDLE_MS;
16652
+ const readBody2 = (request2, limit) => readJsonBody(request2, limit, bodyIdleMs);
16653
+ const infoLimit = new WindowLimiter({ max: INFO_PER_MINUTE, windowMs: MINUTE_MS });
16654
+ const pairingResultLimit = new WindowLimiter({ max: PAIRING_RESULTS_PER_MINUTE, windowMs: MINUTE_MS });
16655
+ const failedAuthenticationLimit = new WindowLimiter({ max: FAILED_AUTHENTICATIONS_PER_MINUTE, windowMs: MINUTE_MS });
16656
+ const clientLimit = new WindowLimiter({ max: AUTHENTICATED_PER_MINUTE, windowMs: MINUTE_MS });
16657
+ const anonymousApi = new ConcurrencyLimiter(ANONYMOUS_IN_FLIGHT);
16658
+ const anonymousAdmin = new ConcurrencyLimiter(ANONYMOUS_IN_FLIGHT);
16659
+ const largeBodies = new ConcurrencyLimiter(MAX_LARGE_BODIES);
16660
+ const gatewayAgent = new Agent({ keepAlive: false });
16661
+ closers.push(() => gatewayAgent.destroy());
16662
+ const tunnelTrusted = () => Boolean(data2.config.publicUrl) || Boolean(data2.config.allowRemoteAdmin && data2.config.adminPublicUrl) || tunnel.status().enabled;
16663
+ const anonymousRefusal = () => new HttpError(429, "rate_limit", "Too many simultaneous requests from this address: wait a moment and try again.", { "retry-after": "1" });
15690
16664
  const streams = /* @__PURE__ */ new Map();
15691
16665
  const dropStaleStreams = () => {
15692
16666
  for (const [stream, client] of streams) {
@@ -15708,6 +16682,7 @@ async function startNarrariumServer(options) {
15708
16682
  };
15709
16683
  const pendingBookUpdates = /* @__PURE__ */ new Set();
15710
16684
  let bookTimer = null;
16685
+ const summariesInFlight = /* @__PURE__ */ new Set();
15711
16686
  const books = new BookRegistry(data2, {
15712
16687
  gitAvailable,
15713
16688
  ai: options.ai,
@@ -15723,7 +16698,8 @@ async function startNarrariumServer(options) {
15723
16698
  pendingBookUpdates.clear();
15724
16699
  for (const id of ids) {
15725
16700
  if (!books.has(id)) continue;
15726
- void books.summary(books.get(id)).then((summary) => broadcast({ event: "book", data: summary })).catch(() => void 0);
16701
+ const task = books.summary(books.get(id)).then((summary) => broadcast({ event: "book", data: summary })).catch(() => void 0).finally(() => summariesInFlight.delete(task));
16702
+ summariesInFlight.add(task);
15727
16703
  }
15728
16704
  }, 300);
15729
16705
  }
@@ -15774,9 +16750,24 @@ async function startNarrariumServer(options) {
15774
16750
  }, 1e3);
15775
16751
  maintenanceTimer.unref();
15776
16752
  };
16753
+ let cachedRelease;
16754
+ const releaseCheck = (fresh) => {
16755
+ if (!fresh && cachedRelease && cachedRelease.version === SERVER_VERSION && cachedRelease.expiresAt > Date.now()) return cachedRelease.check;
16756
+ const entry = { version: SERVER_VERSION, expiresAt: Number.POSITIVE_INFINITY, check: checkRelease(SERVER_VERSION) };
16757
+ cachedRelease = entry;
16758
+ entry.check.then(
16759
+ (result) => {
16760
+ entry.expiresAt = Date.now() + (result.reason === "unavailable" ? RELEASE_UNREACHABLE_CACHE_MS : RELEASE_CACHE_MS);
16761
+ },
16762
+ () => {
16763
+ if (cachedRelease === entry) cachedRelease = void 0;
16764
+ }
16765
+ );
16766
+ return entry.check;
16767
+ };
15777
16768
  const updateInfo = async (client) => {
15778
16769
  const [release, lastUpdate, pending] = await Promise.all([
15779
- checkRelease(SERVER_VERSION),
16770
+ releaseCheck(false),
15780
16771
  installer.readUpdateResult().catch(() => void 0),
15781
16772
  pendingRequest()
15782
16773
  ]);
@@ -15804,12 +16795,12 @@ async function startNarrariumServer(options) {
15804
16795
  const version = body.version;
15805
16796
  if (!isExactSemver(version) || body.confirmed !== true) throw new HttpError(400, "invalid_request", "Send the exact version to install and confirm it.");
15806
16797
  if (body.installedVersion !== void 0 && body.installedVersion !== SERVER_VERSION) throw new HttpError(412, "version", "The server version changed: reload before trying again.");
15807
- const release = await checkRelease(SERVER_VERSION);
16798
+ const release = await releaseCheck(true);
15808
16799
  if (!release.available || !release.signed || release.latestVersion !== version) throw new HttpError(409, "release", "The requested version is not the latest signed and attested npm release: no update was started.");
15809
16800
  await books.requestUpdate(async () => {
15810
16801
  const previous = await pendingRequest();
15811
16802
  if (previous && previous.version !== version) throw new HttpError(409, "update_pending", "A different update is already waiting for the supervisor.");
15812
- const requestId = previous?.requestId ?? randomUUID6();
16803
+ const requestId = previous?.requestId ?? randomUUID8();
15813
16804
  if (!previous) await installer.writeUpdateRequest({ schemaVersion: 1, packageName: SERVER_PACKAGE_NAME, version, requestedAt: (/* @__PURE__ */ new Date()).toISOString(), requestId, source });
15814
16805
  watchMaintenance(requestId);
15815
16806
  });
@@ -15871,11 +16862,22 @@ async function startNarrariumServer(options) {
15871
16862
  pattern: /^\/api\/v1\/books\/([^/]+)\/files\/read$/,
15872
16863
  scope: "read",
15873
16864
  handler: async ({ request: request2, params }) => {
16865
+ const maxPaths = 100;
16866
+ const maxBytes = 16 * 1024 * 1024;
15874
16867
  const runtime = books.get(params[0]);
15875
16868
  const body = asObject(await readBody2(request2, SMALL_BODY));
15876
- const paths = Array.isArray(body.paths) ? body.paths.filter((entry) => typeof entry === "string").slice(0, 2e3) : [];
15877
- const files = await Promise.all(paths.map((path10) => runtime.store.read(path10)));
15878
- return { files: files.filter(Boolean) };
16869
+ const paths = [...new Set(Array.isArray(body.paths) ? body.paths.filter((entry) => typeof entry === "string") : [])];
16870
+ if (paths.length > maxPaths) throw new HttpError(400, "too_many_paths", `Read at most ${maxPaths} different files per request.`);
16871
+ const files = [];
16872
+ let total = 0;
16873
+ for (const filePath of paths) {
16874
+ const file = await runtime.store.read(filePath);
16875
+ if (!file) continue;
16876
+ total += Buffer.byteLength(file.content, "utf8");
16877
+ if (total > maxBytes) throw new HttpError(413, "payload_too_large", "These files are too large to be read in one request: ask for fewer of them.");
16878
+ files.push(file);
16879
+ }
16880
+ return { files };
15879
16881
  }
15880
16882
  },
15881
16883
  {
@@ -15885,7 +16887,15 @@ async function startNarrariumServer(options) {
15885
16887
  handler: async ({ request: request2, params }) => {
15886
16888
  books.assertNotInMaintenance();
15887
16889
  const runtime = books.get(params[0]);
15888
- const body = asObject(await readBody2(request2, LARGE_BODY));
16890
+ const releaseBody = largeBodies.enter("commit");
16891
+ if (!releaseBody) throw new HttpError(503, "busy", "The server is already receiving large uploads: try again in a moment.", { "retry-after": "2" });
16892
+ let body;
16893
+ try {
16894
+ body = asObject(await readBody2(request2, LARGE_BODY));
16895
+ } finally {
16896
+ releaseBody();
16897
+ }
16898
+ if (Array.isArray(body.changes) && body.changes.length > MAX_COMMIT_CHANGES) throw new HttpError(413, "too_many_changes", `A commit may change at most ${MAX_COMMIT_CHANGES} files: send the rest in another commit.`);
15889
16899
  const changes = Array.isArray(body.changes) ? body.changes.map((entry) => {
15890
16900
  const change = asObject(entry);
15891
16901
  if (typeof change.path !== "string" || change.content !== null && typeof change.content !== "string") throw new HttpError(400, "invalid_change", "Every change needs a path and a text content (or null).");
@@ -15997,13 +17007,13 @@ async function startNarrariumServer(options) {
15997
17007
  method: "POST",
15998
17008
  pattern: /^\/api\/v1\/books\/([^/]+)\/director\/ask$/,
15999
17009
  scope: "run",
16000
- handler: async ({ request: request2, params }) => {
17010
+ handler: async ({ request: request2, response, params }) => {
16001
17011
  books.assertNotInMaintenance();
16002
17012
  const runtime = books.get(params[0]);
16003
17013
  const body = asObject(await readBody2(request2, SMALL_BODY));
16004
17014
  const controller = new AbortController();
16005
- request2.on("close", () => {
16006
- if (!request2.complete || request2.destroyed) controller.abort();
17015
+ response.once("close", () => {
17016
+ if (!response.writableEnded) controller.abort();
16007
17017
  });
16008
17018
  return runtime.session.askDirector({ target: asTarget(body.target), question: typeof body.question === "string" ? body.question.slice(0, 4e3) : void 0, save: body.save === true, signal: controller.signal });
16009
17019
  }
@@ -16011,16 +17021,6 @@ async function startNarrariumServer(options) {
16011
17021
  { method: "GET", pattern: /^\/api\/v1\/updates$/, scope: "read", handler: async ({ client }) => updateInfo(client) },
16012
17022
  { method: "POST", pattern: /^\/api\/v1\/updates$/, scope: "update", handler: async ({ request: request2 }) => requestUpdate(asObject(await readBody2(request2, 4096)), "client") }
16013
17023
  ];
16014
- const limits = /* @__PURE__ */ new Map();
16015
- const limit = (key, max) => {
16016
- let state = limits.get(key);
16017
- if (!state || Date.now() - state.window >= 6e4) {
16018
- state = { window: Date.now(), count: 0 };
16019
- limits.set(key, state);
16020
- }
16021
- state.count += 1;
16022
- if (state.count > max) throw new HttpError(429, "rate_limit", "Too many requests: wait a moment and try again.");
16023
- };
16024
17024
  const common = (response) => {
16025
17025
  response.setHeader("cache-control", "no-store");
16026
17026
  response.setHeader("x-content-type-options", "nosniff");
@@ -16041,7 +17041,7 @@ async function startNarrariumServer(options) {
16041
17041
  if (!allowedOrigins().has(origin)) throw new HttpError(403, "origin_not_allowed", `The origin ${origin} is not allowed. Add it in the server's admin panel (Approved network).`);
16042
17042
  response.setHeader("access-control-allow-origin", origin);
16043
17043
  response.setHeader("vary", "Origin");
16044
- response.setHeader("access-control-expose-headers", "etag");
17044
+ response.setHeader("access-control-expose-headers", "etag, retry-after, x-request-id");
16045
17045
  }
16046
17046
  if (request2.method === "OPTIONS") {
16047
17047
  response.setHeader("access-control-allow-methods", "GET, POST, PUT, DELETE, OPTIONS");
@@ -16052,35 +17052,51 @@ async function startNarrariumServer(options) {
16052
17052
  response.end();
16053
17053
  return;
16054
17054
  }
16055
- const url = new URL(request2.url ?? "/", "http://127.0.0.1");
17055
+ let url;
17056
+ try {
17057
+ url = new URL(request2.url ?? "/", "http://127.0.0.1");
17058
+ } catch {
17059
+ throw new HttpError(400, "invalid_request", "The request address is not valid.");
17060
+ }
17061
+ const source = sourceOf(request2, tunnelTrusted());
17062
+ const releaseAnonymous = anonymousApi.enter(source);
17063
+ if (!releaseAnonymous) throw anonymousRefusal();
17064
+ response.once("close", releaseAnonymous);
16056
17065
  if (request2.method === "GET" && (url.pathname === "/api/v1/info" || url.pathname === "/")) {
16057
- limit("info", 600);
17066
+ refuseOver(infoLimit.hit(source));
16058
17067
  const nonce = url.searchParams.get("nonce");
16059
17068
  if (nonce !== null && !/^[a-f0-9]{64}$/.test(nonce)) throw new HttpError(400, "invalid_nonce", "The nonce must be 64 hexadecimal characters.");
16060
17069
  sendJson(response, 200, url.pathname === "/" ? { ...info(), message: "Narrarium personal server. Connect it from Narrarium \u2192 Settings \u2192 Personal server." } : info(nonce ?? void 0));
16061
17070
  return;
16062
17071
  }
16063
17072
  if (request2.method === "POST" && url.pathname === "/api/v1/pairings") {
16064
- limit("pair", 30);
17073
+ auth.admitPairingAttempt(source);
16065
17074
  const body = asObject(await readBody2(request2, 4096));
16066
17075
  const code = typeof body.code === "string" ? body.code : "";
16067
17076
  const nonce = typeof body.nonce === "string" ? body.nonce : "";
16068
17077
  const label = typeof body.label === "string" ? body.label.trim() : "";
16069
17078
  if (!/^\d{9}$/.test(code) || !/^[a-f0-9]{64}$/.test(nonce) || !label || label.length > 80) throw new HttpError(400, "invalid_request", "Send the nine-digit code, a nonce and a client name.");
16070
- sendJson(response, 200, await auth.requestPairing(code, nonce, label));
17079
+ sendJson(response, 200, await auth.requestPairing(code, nonce, label, source));
16071
17080
  return;
16072
17081
  }
16073
17082
  const pairing = /^\/api\/v1\/pairings\/([0-9a-f-]{36})\/result$/.exec(url.pathname);
16074
17083
  if (request2.method === "POST" && pairing) {
16075
- limit("pair_result", 120);
17084
+ refuseOver(pairingResultLimit.hit(source));
16076
17085
  const body = asObject(await readBody2(request2, 4096));
16077
17086
  if (typeof body.nonce !== "string" || !/^[a-f0-9]{64}$/.test(body.nonce)) throw new HttpError(400, "invalid_request", "Send the pairing nonce.");
16078
17087
  sendJson(response, 200, await auth.pollPairing(pairing[1], body.nonce));
16079
17088
  return;
16080
17089
  }
16081
17090
  const token = /^Bearer\s+(.+)$/i.exec(String(request2.headers.authorization ?? ""))?.[1];
16082
- const client = auth.authenticate(token);
16083
- limit("authenticated", 1200);
17091
+ let client;
17092
+ try {
17093
+ client = auth.authenticate(token);
17094
+ } catch (error) {
17095
+ if (error instanceof HttpError && error.status === 401) refuseOver(failedAuthenticationLimit.hit(source));
17096
+ throw error;
17097
+ }
17098
+ releaseAnonymous();
17099
+ refuseOver(clientLimit.hit(client.id));
16084
17100
  if (request2.method === "DELETE" && url.pathname === "/api/v1/session") {
16085
17101
  await auth.revoke(client.id);
16086
17102
  dropStaleStreams();
@@ -16088,20 +17104,34 @@ async function startNarrariumServer(options) {
16088
17104
  return;
16089
17105
  }
16090
17106
  if (request2.method === "GET" && url.pathname === "/api/v1/events") {
16091
- auth.authenticate(token, "read");
17107
+ assertScope(client, "read");
16092
17108
  if (streams.size >= MAX_STREAMS) throw new HttpError(429, "too_many_streams", "Too many live connections to this server.");
17109
+ if ([...streams.values()].filter((entry) => entry.id === client.id).length >= MAX_STREAMS_PER_CLIENT) {
17110
+ throw new HttpError(429, "too_many_streams", `This client already holds ${MAX_STREAMS_PER_CLIENT} live connections: close one before opening another.`);
17111
+ }
16093
17112
  response.writeHead(200, { "content-type": "text/event-stream; charset=utf-8", connection: "keep-alive", "x-accel-buffering": "no" });
17113
+ request2.socket.setKeepAlive(true, 15e3);
16094
17114
  response.write(encodeServerSentEvent({ event: "hello", data: { serverId: data2.config.serverId, at: (/* @__PURE__ */ new Date()).toISOString() } }));
16095
17115
  streams.set(response, client);
16096
- request2.on("close", () => streams.delete(response));
17116
+ const forget = () => {
17117
+ streams.delete(response);
17118
+ };
17119
+ response.once("close", forget);
17120
+ response.once("error", forget);
16097
17121
  return;
16098
17122
  }
16099
17123
  for (const route of routes) {
16100
17124
  if (route.method !== request2.method) continue;
16101
17125
  const match = route.pattern.exec(url.pathname);
16102
17126
  if (!match) continue;
16103
- auth.authenticate(token, route.scope);
16104
- const params = match.slice(1).map((value) => decodeURIComponent(value));
17127
+ assertScope(client, route.scope);
17128
+ const params = match.slice(1).map((value) => {
17129
+ try {
17130
+ return decodeURIComponent(value);
17131
+ } catch {
17132
+ throw new HttpError(400, "invalid_path", "The address contains an invalid escape sequence.");
17133
+ }
17134
+ });
16105
17135
  const payload = await route.handler({ request: request2, response, params, url, client });
16106
17136
  if (!auth.isCurrent(client)) throw new HttpError(401, "revoked", "This client token was revoked.");
16107
17137
  sendJson(response, url.pathname === "/api/v1/updates" && request2.method === "POST" ? 202 : 200, payload);
@@ -16109,11 +17139,11 @@ async function startNarrariumServer(options) {
16109
17139
  }
16110
17140
  throw new HttpError(404, "not_found", "Unknown endpoint.");
16111
17141
  })().catch((error) => {
16112
- const { status, code, message } = errorStatus(error);
16113
- if (status >= 500) log(`Error on ${request2.method} ${request2.url}: ${message}`);
17142
+ const mapped = errorStatus(error);
17143
+ if (isFailure(mapped)) log(`Error${mapped.requestId ? ` ${mapped.requestId}` : ""} on ${request2.method} ${request2.url}: ${mapped.detail ?? mapped.message}`);
16114
17144
  if (!response.headersSent) {
16115
17145
  response.setHeader("connection", "close");
16116
- sendJson(response, status, { code, message });
17146
+ sendError(response, mapped);
16117
17147
  } else response.end();
16118
17148
  }).finally(() => {
16119
17149
  if (request2.url !== "/api/v1/events") log(`${request2.method} ${request2.url} ${response.statusCode} ${Date.now() - started}ms`);
@@ -16130,34 +17160,29 @@ async function startNarrariumServer(options) {
16130
17160
  const host = String(request2.headers.host ?? "").toLowerCase();
16131
17161
  const remote = Boolean(publicAdminOrigin) && host === hostOf(publicAdminOrigin);
16132
17162
  const local = localOrigins.some((origin) => hostOf(origin) === host);
16133
- const path10 = request2.url ?? "/";
16134
- const bridge = remote && /^\/api\/v1\//.test(path10);
17163
+ const path11 = request2.url ?? "/";
17164
+ const bridge = remote && /^\/api\/v1\//.test(path11);
16135
17165
  if (!local && !remote || request2.headers.origin && !bridge && !adminOrigins.includes(request2.headers.origin)) {
16136
17166
  throw new HttpError(403, "admin_origin", "The admin panel is available only from this computer or from its approved public address.");
16137
17167
  }
16138
17168
  if (bridge) {
16139
- const upstream = httpRequest2({ hostname: "127.0.0.1", port: apiPort, path: path10, method: request2.method, headers: { ...request2.headers, host: hostOf(data2.config.publicUrl) ?? `127.0.0.1:${apiPort}`, "x-narrarium-proxy": "admin-gateway" } }, (upstreamResponse) => {
16140
- response.writeHead(upstreamResponse.statusCode ?? 502, upstreamResponse.headers);
16141
- upstreamResponse.pipe(response);
16142
- });
16143
- upstream.on("error", () => {
16144
- if (!response.headersSent) sendJson(response, 503, { code: "api_proxy", message: "The server API is not reachable." });
16145
- else response.end();
16146
- });
16147
- upstream.setTimeout(13e4, () => upstream.destroy(new Error("timeout")));
16148
- request2.pipe(upstream);
17169
+ bridgeToApi(request2, response, { port: apiPort, host: hostOf(data2.config.publicUrl) ?? `127.0.0.1:${apiPort}` }, gatewayAgent);
16149
17170
  return;
16150
17171
  }
16151
- if (request2.method === "GET" && (path10 === "/" || path10 === "/admin.js" || path10 === "/admin.css")) {
16152
- response.writeHead(200, { "content-type": path10 === "/" ? "text/html; charset=utf-8" : path10 === "/admin.css" ? "text/css; charset=utf-8" : "text/javascript; charset=utf-8" });
16153
- response.end(path10 === "/" ? ADMIN_HTML : path10 === "/admin.css" ? ADMIN_CSS : ADMIN_JS);
17172
+ if (request2.method === "GET" && (path11 === "/" || path11 === "/admin.js" || path11 === "/admin.css")) {
17173
+ response.writeHead(200, { "content-type": path11 === "/" ? "text/html; charset=utf-8" : path11 === "/admin.css" ? "text/css; charset=utf-8" : "text/javascript; charset=utf-8" });
17174
+ response.end(path11 === "/" ? ADMIN_HTML : path11 === "/admin.css" ? ADMIN_CSS : ADMIN_JS);
16154
17175
  return;
16155
17176
  }
16156
17177
  if (request2.method === "POST" && !adminOrigins.includes(header(request2, "origin") ?? "")) throw new HttpError(403, "admin_csrf", "This admin request does not come from the admin panel.");
16157
17178
  const cookieName = `nrs_admin_${adminPort}`;
16158
- if (path10 === "/admin/api/login" && request2.method === "POST") {
17179
+ if (path11 === "/admin/api/login" && request2.method === "POST") {
17180
+ const source = sourceOf(request2, tunnelTrusted());
17181
+ const releaseAnonymous = anonymousAdmin.enter(source);
17182
+ if (!releaseAnonymous) throw anonymousRefusal();
17183
+ response.once("close", releaseAnonymous);
16159
17184
  const body2 = asObject(await readBody2(request2, 4096));
16160
- const session = await auth.login(typeof body2.code === "string" ? body2.code.trim() : "");
17185
+ const session = await auth.login(typeof body2.code === "string" ? body2.code.trim() : "", source);
16161
17186
  const secure = header(request2, "origin")?.startsWith("https://") ? "; Secure" : "";
16162
17187
  response.setHeader("set-cookie", `${cookieName}=${session.cookie}; HttpOnly; SameSite=Strict; Path=/admin/; Max-Age=1800${secure}`);
16163
17188
  sendJson(response, 200, { csrf: session.csrf });
@@ -16166,13 +17191,13 @@ async function startNarrariumServer(options) {
16166
17191
  const cookie = new RegExp(`(?:^|;\\s*)${cookieName}=([A-Za-z0-9_-]+)`).exec(header(request2, "cookie") ?? "")?.[1];
16167
17192
  auth.adminSession(cookie, request2.method === "POST" ? header(request2, "x-csrf-token") ?? "" : void 0);
16168
17193
  const body = request2.method === "POST" ? asObject(await readBody2(request2, 16384)) : {};
16169
- if (path10 === "/admin/api/logout" && request2.method === "POST") {
17194
+ if (path11 === "/admin/api/logout" && request2.method === "POST") {
16170
17195
  auth.logout(cookie);
16171
17196
  response.setHeader("set-cookie", `${cookieName}=; HttpOnly; SameSite=Strict; Path=/admin/; Max-Age=0`);
16172
17197
  sendJson(response, 200, { ok: true });
16173
17198
  return;
16174
17199
  }
16175
- if (path10 === "/admin/api/status" && request2.method === "GET") {
17200
+ if (path11 === "/admin/api/status" && request2.method === "GET") {
16176
17201
  const summaries = await books.summaries().catch(() => []);
16177
17202
  sendJson(response, 200, {
16178
17203
  serverId: data2.config.serverId,
@@ -16200,43 +17225,43 @@ async function startNarrariumServer(options) {
16200
17225
  });
16201
17226
  return;
16202
17227
  }
16203
- if (path10 === "/admin/api/admin-code/rotate" && request2.method === "POST") {
17228
+ if (path11 === "/admin/api/admin-code/rotate" && request2.method === "POST") {
16204
17229
  sendJson(response, 200, { code: await auth.rotateAdminCode() });
16205
17230
  return;
16206
17231
  }
16207
- if (path10 === "/admin/api/admin-code/set" && request2.method === "POST") {
17232
+ if (path11 === "/admin/api/admin-code/set" && request2.method === "POST") {
16208
17233
  await auth.replaceAdminCode(typeof body.code === "string" ? body.code : "");
16209
17234
  sendJson(response, 200, { changed: true });
16210
17235
  return;
16211
17236
  }
16212
- if (path10 === "/admin/api/token" && request2.method === "POST") {
17237
+ if (path11 === "/admin/api/token" && request2.method === "POST") {
16213
17238
  const replaceId = typeof body.replaceId === "string" ? body.replaceId : void 0;
16214
17239
  sendJson(response, 200, await auth.issue(typeof body.label === "string" ? body.label : "", parseScopes(body.scopes), replaceId));
16215
17240
  if (replaceId) dropStaleStreams();
16216
17241
  return;
16217
17242
  }
16218
- if (path10 === "/admin/api/revoke" && request2.method === "POST") {
17243
+ if (path11 === "/admin/api/revoke" && request2.method === "POST") {
16219
17244
  if (typeof body.id !== "string") throw new HttpError(400, "invalid_request", "Which client?");
16220
17245
  sendJson(response, 200, { revoked: await auth.revoke(body.id) });
16221
17246
  dropStaleStreams();
16222
17247
  return;
16223
17248
  }
16224
- if (path10 === "/admin/api/pairing" && request2.method === "POST") {
17249
+ if (path11 === "/admin/api/pairing" && request2.method === "POST") {
16225
17250
  sendJson(response, 200, await auth.startPairing());
16226
17251
  return;
16227
17252
  }
16228
- if (path10 === "/admin/api/pairing/approve" && request2.method === "POST") {
17253
+ if (path11 === "/admin/api/pairing/approve" && request2.method === "POST") {
16229
17254
  if (typeof body.id !== "string") throw new HttpError(400, "invalid_request", "Which pairing?");
16230
17255
  await auth.approvePairing(body.id, { allowUpdate: body.allowUpdate === true });
16231
17256
  sendJson(response, 200, { approved: true });
16232
17257
  return;
16233
17258
  }
16234
- if (path10 === "/admin/api/pairing/cancel" && request2.method === "POST") {
17259
+ if (path11 === "/admin/api/pairing/cancel" && request2.method === "POST") {
16235
17260
  await auth.cancelPairing();
16236
17261
  sendJson(response, 200, { cancelled: true });
16237
17262
  return;
16238
17263
  }
16239
- if (path10 === "/admin/api/network" && request2.method === "POST") {
17264
+ if (path11 === "/admin/api/network" && request2.method === "POST") {
16240
17265
  const list = (value, max) => (Array.isArray(value) ? value : []).filter((entry) => typeof entry === "string" && entry.trim().length > 0).slice(0, max);
16241
17266
  const origins = list(body.origins, 20).map((origin) => normalizeOrigin(origin));
16242
17267
  const allowedAiBases = list(body.allowedAiBases, 50).map((base) => normalizeOrigin(base));
@@ -16255,16 +17280,16 @@ async function startNarrariumServer(options) {
16255
17280
  sendJson(response, 200, { saved: true });
16256
17281
  return;
16257
17282
  }
16258
- if ((path10 === "/admin/api/ai/approve" || path10 === "/admin/api/ai/dismiss") && request2.method === "POST") {
17283
+ if ((path11 === "/admin/api/ai/approve" || path11 === "/admin/api/ai/dismiss") && request2.method === "POST") {
16259
17284
  const bases = (Array.isArray(body.bases) ? body.bases : []).filter((entry) => typeof entry === "string").slice(0, 50).map((base) => normalizeOrigin(base));
16260
17285
  await data2.updateConfig((next) => {
16261
- if (path10.endsWith("approve")) next.allowedAiBases = [.../* @__PURE__ */ new Set([...next.allowedAiBases, ...bases])].slice(0, 50);
17286
+ if (path11.endsWith("approve")) next.allowedAiBases = [.../* @__PURE__ */ new Set([...next.allowedAiBases, ...bases])].slice(0, 50);
16262
17287
  next.pendingAiBases = next.pendingAiBases.filter((base) => !bases.includes(base));
16263
17288
  });
16264
17289
  sendJson(response, 200, { saved: true });
16265
17290
  return;
16266
17291
  }
16267
- if (path10 === "/admin/api/tunnel/configure" && request2.method === "POST") {
17292
+ if (path11 === "/admin/api/tunnel/configure" && request2.method === "POST") {
16268
17293
  try {
16269
17294
  await tunnel.configure({ token: typeof body.token === "string" ? body.token : "", hostname: typeof body.hostname === "string" ? body.hostname : void 0 });
16270
17295
  } catch (error) {
@@ -16273,38 +17298,38 @@ async function startNarrariumServer(options) {
16273
17298
  sendJson(response, 200, await tunnel.start());
16274
17299
  return;
16275
17300
  }
16276
- if (path10 === "/admin/api/tunnel/restart" && request2.method === "POST") {
17301
+ if (path11 === "/admin/api/tunnel/restart" && request2.method === "POST") {
16277
17302
  sendJson(response, 200, await tunnel.restart());
16278
17303
  return;
16279
17304
  }
16280
- if (path10 === "/admin/api/tunnel/remove" && request2.method === "POST") {
17305
+ if (path11 === "/admin/api/tunnel/remove" && request2.method === "POST") {
16281
17306
  sendJson(response, 200, await tunnel.remove());
16282
17307
  return;
16283
17308
  }
16284
- if (path10 === "/admin/api/updates" && request2.method === "GET") {
17309
+ if (path11 === "/admin/api/updates" && request2.method === "GET") {
16285
17310
  sendJson(response, 200, await updateInfo());
16286
17311
  return;
16287
17312
  }
16288
- if (path10 === "/admin/api/updates" && request2.method === "POST") {
17313
+ if (path11 === "/admin/api/updates" && request2.method === "POST") {
16289
17314
  sendJson(response, 202, await requestUpdate({ ...body, confirmed: true }, "admin"));
16290
17315
  return;
16291
17316
  }
16292
- if (path10 === "/admin/api/scopes" && request2.method === "GET") {
17317
+ if (path11 === "/admin/api/scopes" && request2.method === "GET") {
16293
17318
  sendJson(response, 200, { scopes: ["read", "write", "run", "update"].filter(isScope) });
16294
17319
  return;
16295
17320
  }
16296
17321
  throw new HttpError(404, "not_found", "Unknown admin endpoint.");
16297
17322
  })().catch((error) => {
16298
- const { status, code, message } = errorStatus(error);
16299
- if (status >= 500) log(`Admin error on ${request2.method} ${request2.url}: ${message}`);
17323
+ const mapped = errorStatus(error);
17324
+ if (isFailure(mapped)) log(`Admin error${mapped.requestId ? ` ${mapped.requestId}` : ""} on ${request2.method} ${request2.url}: ${mapped.detail ?? mapped.message}`);
16300
17325
  if (!response.headersSent) {
16301
17326
  response.setHeader("connection", "close");
16302
- sendJson(response, status, { code, message });
17327
+ sendError(response, mapped);
16303
17328
  } else response.end();
16304
17329
  });
16305
17330
  });
16306
17331
  for (const server of [api, admin]) {
16307
- server.requestTimeout = 0;
17332
+ server.requestTimeout = REQUEST_TIMEOUT_MS;
16308
17333
  server.headersTimeout = 2e4;
16309
17334
  server.keepAliveTimeout = 5e3;
16310
17335
  server.maxHeadersCount = 60;
@@ -16331,6 +17356,7 @@ async function startNarrariumServer(options) {
16331
17356
  books,
16332
17357
  auth,
16333
17358
  tunnel,
17359
+ servers: { api, admin },
16334
17360
  allowedBases: () => [...data2.config.allowedAiBases],
16335
17361
  async close() {
16336
17362
  if (closed) return;
@@ -16340,6 +17366,7 @@ async function startNarrariumServer(options) {
16340
17366
  if (bookTimer) clearTimeout(bookTimer);
16341
17367
  for (const stream of streams.keys()) stream.end();
16342
17368
  for (const close of closers.reverse()) await close();
17369
+ await Promise.allSettled([...summariesInFlight]);
16343
17370
  await Promise.all([api, admin].map((server) => new Promise((resolve) => {
16344
17371
  server.close(() => resolve());
16345
17372
  server.closeAllConnections?.();
@@ -16360,7 +17387,7 @@ var dataRoot;
16360
17387
  var verbose = false;
16361
17388
  var valid = true;
16362
17389
  for (let index = 0; index < args.length; index += 1) {
16363
- if (args[index] === "--data-root" && args[index + 1] && !args[index + 1].startsWith("--")) dataRoot = path9.resolve(args[++index]);
17390
+ if (args[index] === "--data-root" && args[index + 1] && !args[index + 1].startsWith("--")) dataRoot = path10.resolve(args[++index]);
16364
17391
  else if (args[index] === "--verbose") verbose = true;
16365
17392
  else valid = false;
16366
17393
  }
@@ -16370,18 +17397,35 @@ if (!valid || !dataRoot) {
16370
17397
  } else {
16371
17398
  let server;
16372
17399
  let restoreNetwork;
16373
- let stopping = false;
16374
- const stop = async () => {
16375
- if (stopping) return;
16376
- stopping = true;
16377
- try {
16378
- await server?.close();
16379
- } finally {
16380
- restoreNetwork?.();
16381
- }
17400
+ let stopped;
17401
+ const stop = () => {
17402
+ stopped ??= (async () => {
17403
+ try {
17404
+ await server?.close();
17405
+ } finally {
17406
+ restoreNetwork?.();
17407
+ }
17408
+ })();
17409
+ return stopped;
16382
17410
  };
16383
17411
  process.once("SIGINT", () => void stop());
16384
17412
  process.once("SIGTERM", () => void stop());
17413
+ const report = (kind, reason) => {
17414
+ const detail = reason instanceof Error ? `${reason.name}: ${reason.message.slice(0, 500)}
17415
+ ${(reason.stack ?? "").split("\n").filter((line) => /^\s+at /.test(line)).slice(0, 5).join("\n")}`.trimEnd() : String(reason).slice(0, 500);
17416
+ process.stderr.write(`[${(/* @__PURE__ */ new Date()).toISOString()}] ${kind}: ${detail}
17417
+ `);
17418
+ };
17419
+ process.on("unhandledRejection", (reason) => report("Unhandled rejection", reason));
17420
+ let crashing = false;
17421
+ process.on("uncaughtException", (error) => {
17422
+ report("Uncaught exception", error);
17423
+ if (crashing) return;
17424
+ crashing = true;
17425
+ process.exitCode = 1;
17426
+ setTimeout(() => process.exit(1), 5e3).unref();
17427
+ void stop().catch(() => void 0).finally(() => process.exit(1));
17428
+ });
16385
17429
  try {
16386
17430
  server = await startNarrariumServer({
16387
17431
  dataDir: dataRoot,
@@ -16391,7 +17435,7 @@ if (!valid || !dataRoot) {
16391
17435
  }
16392
17436
  });
16393
17437
  restoreNetwork = installOutboundNetwork(server.allowedBases);
16394
- const rootFlag = path9.resolve(defaultDataRoot()) === dataRoot ? "" : ` --data-root "${dataRoot}"`;
17438
+ const rootFlag = path10.resolve(defaultDataRoot()) === dataRoot ? "" : ` --data-root "${dataRoot}"`;
16395
17439
  const lines = [
16396
17440
  `Narrarium personal server ${SERVER_VERSION}${process.env[SUPERVISED_ENV] === "1" ? " (supervised)" : ""}`,
16397
17441
  ` API: ${server.url}`,