daftari 3.11.0 → 3.12.1

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.
Files changed (119) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +210 -937
  3. package/dist/access/rbac.d.ts +1 -0
  4. package/dist/access/rbac.d.ts.map +1 -1
  5. package/dist/access/rbac.js +5 -0
  6. package/dist/access/rbac.js.map +1 -1
  7. package/dist/distill/state.d.ts +9 -1
  8. package/dist/distill/state.d.ts.map +1 -1
  9. package/dist/distill/state.js +58 -7
  10. package/dist/distill/state.js.map +1 -1
  11. package/dist/eval/llm-openrouter.d.ts.map +1 -1
  12. package/dist/eval/llm-openrouter.js +5 -22
  13. package/dist/eval/llm-openrouter.js.map +1 -1
  14. package/dist/hooks/loader.d.ts.map +1 -1
  15. package/dist/hooks/loader.js +8 -0
  16. package/dist/hooks/loader.js.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +47 -14
  20. package/dist/index.js.map +1 -1
  21. package/dist/integrations/distill.d.ts +16 -0
  22. package/dist/integrations/distill.d.ts.map +1 -0
  23. package/dist/integrations/distill.js +164 -0
  24. package/dist/integrations/distill.js.map +1 -0
  25. package/dist/integrations/engine.d.ts +134 -0
  26. package/dist/integrations/engine.d.ts.map +1 -0
  27. package/dist/integrations/engine.js +571 -0
  28. package/dist/integrations/engine.js.map +1 -0
  29. package/dist/integrations/google.d.ts +13 -0
  30. package/dist/integrations/google.d.ts.map +1 -0
  31. package/dist/integrations/google.js +551 -0
  32. package/dist/integrations/google.js.map +1 -0
  33. package/dist/integrations/notion.d.ts +16 -0
  34. package/dist/integrations/notion.d.ts.map +1 -0
  35. package/dist/integrations/notion.js +485 -0
  36. package/dist/integrations/notion.js.map +1 -0
  37. package/dist/integrations/oauth.d.ts +14 -0
  38. package/dist/integrations/oauth.d.ts.map +1 -0
  39. package/dist/integrations/oauth.js +141 -0
  40. package/dist/integrations/oauth.js.map +1 -0
  41. package/dist/integrations/queue.d.ts +30 -0
  42. package/dist/integrations/queue.d.ts.map +1 -0
  43. package/dist/integrations/queue.js +255 -0
  44. package/dist/integrations/queue.js.map +1 -0
  45. package/dist/integrations/review.d.ts +5 -0
  46. package/dist/integrations/review.d.ts.map +1 -0
  47. package/dist/integrations/review.js +36 -0
  48. package/dist/integrations/review.js.map +1 -0
  49. package/dist/integrations/routes.d.ts +26 -0
  50. package/dist/integrations/routes.d.ts.map +1 -0
  51. package/dist/integrations/routes.js +258 -0
  52. package/dist/integrations/routes.js.map +1 -0
  53. package/dist/integrations/runtime.d.ts +33 -0
  54. package/dist/integrations/runtime.d.ts.map +1 -0
  55. package/dist/integrations/runtime.js +280 -0
  56. package/dist/integrations/runtime.js.map +1 -0
  57. package/dist/integrations/state.d.ts +10 -0
  58. package/dist/integrations/state.d.ts.map +1 -0
  59. package/dist/integrations/state.js +238 -0
  60. package/dist/integrations/state.js.map +1 -0
  61. package/dist/integrations/types.d.ts +45 -0
  62. package/dist/integrations/types.d.ts.map +1 -0
  63. package/dist/integrations/types.js +4 -0
  64. package/dist/integrations/types.js.map +1 -0
  65. package/dist/lifecycle/lock.d.ts.map +1 -1
  66. package/dist/lifecycle/lock.js +15 -1
  67. package/dist/lifecycle/lock.js.map +1 -1
  68. package/dist/resources.js +5 -1
  69. package/dist/resources.js.map +1 -1
  70. package/dist/search/coverage.d.ts.map +1 -1
  71. package/dist/search/coverage.js +5 -5
  72. package/dist/search/coverage.js.map +1 -1
  73. package/dist/search/graph-expansion.d.ts +55 -0
  74. package/dist/search/graph-expansion.d.ts.map +1 -0
  75. package/dist/search/graph-expansion.js +160 -0
  76. package/dist/search/graph-expansion.js.map +1 -0
  77. package/dist/search/hybrid.d.ts +4 -0
  78. package/dist/search/hybrid.d.ts.map +1 -1
  79. package/dist/search/hybrid.js.map +1 -1
  80. package/dist/serve/index.d.ts +16 -0
  81. package/dist/serve/index.d.ts.map +1 -1
  82. package/dist/serve/index.js +325 -18
  83. package/dist/serve/index.js.map +1 -1
  84. package/dist/serve/limits.d.ts +13 -0
  85. package/dist/serve/limits.d.ts.map +1 -1
  86. package/dist/serve/limits.js +47 -0
  87. package/dist/serve/limits.js.map +1 -1
  88. package/dist/serve/proxy-trust.d.ts +19 -0
  89. package/dist/serve/proxy-trust.d.ts.map +1 -0
  90. package/dist/serve/proxy-trust.js +167 -0
  91. package/dist/serve/proxy-trust.js.map +1 -0
  92. package/dist/storage/backends/fs.d.ts.map +1 -1
  93. package/dist/storage/backends/fs.js +13 -0
  94. package/dist/storage/backends/fs.js.map +1 -1
  95. package/dist/storage/sync.d.ts.map +1 -1
  96. package/dist/storage/sync.js +11 -0
  97. package/dist/storage/sync.js.map +1 -1
  98. package/dist/tools/board.test.js +3 -0
  99. package/dist/tools/board.test.js.map +1 -1
  100. package/dist/tools/read.d.ts.map +1 -1
  101. package/dist/tools/read.js +7 -1
  102. package/dist/tools/read.js.map +1 -1
  103. package/dist/tools/search.d.ts.map +1 -1
  104. package/dist/tools/search.js +31 -3
  105. package/dist/tools/search.js.map +1 -1
  106. package/dist/utils/config.d.ts +15 -0
  107. package/dist/utils/config.d.ts.map +1 -1
  108. package/dist/utils/config.js +172 -5
  109. package/dist/utils/config.js.map +1 -1
  110. package/dist/utils/realpath-confine.d.ts +4 -0
  111. package/dist/utils/realpath-confine.d.ts.map +1 -0
  112. package/dist/utils/realpath-confine.js +61 -0
  113. package/dist/utils/realpath-confine.js.map +1 -0
  114. package/dist/utils/vault-gitignore.d.ts +1 -1
  115. package/dist/utils/vault-gitignore.d.ts.map +1 -1
  116. package/dist/utils/vault-gitignore.js +23 -3
  117. package/dist/utils/vault-gitignore.js.map +1 -1
  118. package/package.json +2 -2
  119. package/templates/config.yaml +3 -0
@@ -28,13 +28,15 @@ import { resolve } from "node:path";
28
28
  import { toNodeHandler } from "@modelcontextprotocol/node";
29
29
  import { createMcpHandler } from "@modelcontextprotocol/server";
30
30
  import { createRemoteJWKSet, jwtVerify } from "jose";
31
- import { GUEST_ROLE, resolveAccess } from "../access/rbac.js";
31
+ import { canManageIntegrations, GUEST_ROLE, resolveAccess, } from "../access/rbac.js";
32
32
  import { loadAttestKey } from "../attest/sign.js";
33
33
  import { listBoard } from "../board/board.js";
34
34
  import { ok } from "../frontmatter/types.js";
35
35
  import { installShutdownHandlers, parseFlag, startVaultServices } from "../index.js";
36
- import { acquireLock } from "../lifecycle/lock.js";
36
+ import { createConfiguredIntegrationRuntime, } from "../integrations/runtime.js";
37
+ import { acquireLock, releaseLock } from "../lifecycle/lock.js";
37
38
  import { setCoverageEnabled } from "../search/coverage.js";
39
+ import { setGraphExpandConfig } from "../search/graph-expansion.js";
38
40
  import { setDefaultWeights, setVecKnnK } from "../search/hybrid.js";
39
41
  import { setSuppressSuperseded } from "../search/suppression.js";
40
42
  import { setProvider } from "../search/vector.js";
@@ -44,9 +46,11 @@ import { directoryExists } from "../storage/local.js";
44
46
  import { syncVault } from "../storage/sync.js";
45
47
  import { vaultBoardDispose, vaultBoardResolve } from "../tools/board.js";
46
48
  import { loadConfig } from "../utils/config.js";
49
+ import { ensureVaultGitignore } from "../utils/vault-gitignore.js";
47
50
  import { renderBoardPage } from "../view/board-page.js";
48
51
  import { appendAuthEvent, tokenHint } from "./auth-log.js";
49
- import { chargePenalty, makeBucket, makePenaltyBox, makeSlotGate, penaltyAllows, releaseSlot, tryAcquireSlot, tryTake, } from "./limits.js";
52
+ import { chargePenalty, makeBucket, makeBucketRegistry, makePenaltyBox, makeSlotGate, penaltyAllows, releaseSlot, takeFromRegistry, tryAcquireSlot, tryTake, } from "./limits.js";
53
+ import { parseTrustedProxies, resolvePublicRemote } from "./proxy-trust.js";
50
54
  import { signSession, verifySession } from "./session.js";
51
55
  export const DEFAULT_PORT = 8787;
52
56
  export const DEFAULT_BIND = "127.0.0.1";
@@ -89,6 +93,11 @@ Exit codes: 2 config/usage error, 3 runtime error.
89
93
  // startup rather than mint forgeable cookies.
90
94
  const MIN_SESSION_KEY_BYTES = 32;
91
95
  const LOOPBACK_BINDS = new Set(["127.0.0.1", "::1", "localhost"]);
96
+ export function httpCallbackBase(bind, port) {
97
+ const callbackHost = isLoopbackBind(bind) ? bind : "127.0.0.1";
98
+ const authority = callbackHost.includes(":") ? `[${callbackHost}]` : callbackHost;
99
+ return `http://${authority}:${port}`;
100
+ }
92
101
  function remoteOf(req) {
93
102
  return req.socket.remoteAddress ?? "unknown";
94
103
  }
@@ -410,6 +419,55 @@ function readBody(req) {
410
419
  });
411
420
  });
412
421
  }
422
+ // Absolute wall-clock deadline for receiving a full request body. Bounds a
423
+ // slow-loris that dribbles bytes under the size cap forever; a stalled body is
424
+ // abandoned and the socket destroyed.
425
+ const MCP_BODY_DEADLINE_MS = 30_000;
426
+ // Read a request body under a HARD byte cap and an absolute deadline. Unlike
427
+ // readBody (which only size-caps and collapses every failure to null), this
428
+ // distinguishes too-large from timed-out from errored so the caller can answer
429
+ // 413 / 408 / 400. It exists for the /mcp path: the MCP adapter's toWebRequest
430
+ // buffers the whole stream with `for await (const chunk of req)` and no ceiling,
431
+ // so the bound must be enforced here, before the adapter runs. The caller then
432
+ // hands the parsed value to the adapter as `parsedBody` and the adapter reads
433
+ // nothing from `req`.
434
+ export function readBodyBounded(req, maxBytes, deadlineMs) {
435
+ return new Promise((resolve) => {
436
+ // Cheap pre-check: an honest oversized upload declares its length, so we
437
+ // refuse it without reading (or buffering) a single byte.
438
+ const declared = Number(req.headers["content-length"]);
439
+ if (Number.isFinite(declared) && declared > maxBytes) {
440
+ resolve({ status: "too_large" });
441
+ return;
442
+ }
443
+ const chunks = [];
444
+ let total = 0;
445
+ let settled = false;
446
+ const finish = (result) => {
447
+ if (settled)
448
+ return;
449
+ settled = true;
450
+ clearTimeout(timer);
451
+ resolve(result);
452
+ };
453
+ // The reader never destroys the socket — it is shared with the response, so
454
+ // the CALLER answers 413/408 with `Connection: close`. Past the cap we stop
455
+ // BUFFERING (memory stays bounded) but keep the `data` listener attached so
456
+ // the rest of the body drains into nowhere rather than back-pressuring.
457
+ // A declared length can lie (or be absent on a chunked body): count the
458
+ // bytes actually delivered and cut the moment they cross the cap.
459
+ const timer = setTimeout(() => finish({ status: "timeout" }), deadlineMs);
460
+ req.on("data", (chunk) => {
461
+ total += chunk.length;
462
+ if (total > maxBytes)
463
+ finish({ status: "too_large" });
464
+ else if (!settled)
465
+ chunks.push(chunk);
466
+ });
467
+ req.on("end", () => finish({ status: "ok", body: Buffer.concat(chunks).toString("utf-8") }));
468
+ req.on("error", () => finish({ status: "error" }));
469
+ });
470
+ }
413
471
  // Parse BoardFilters from a URLSearchParams instance (query string).
414
472
  function filtersFromQuery(query) {
415
473
  const filters = {};
@@ -462,22 +520,37 @@ export function startHttpServer(vaultRoot, config, tokens, bind, port, opts = {}
462
520
  // (.daftari/process.lock), the principal set is config-declared and
463
521
  // finite, and a restart re-arms the floor immediately.
464
522
  const limits = config.server.limits;
523
+ // Parsed once: which immediate peers are our own proxies, so X-Forwarded-For
524
+ // is honored only from them (finding F4).
525
+ const trustedProxies = parseTrustedProxies(config.server.trustedProxies);
465
526
  const penaltyBox = makePenaltyBox(limits.authFailureBurst, limits.authFailuresPerMinute);
466
527
  const principalBuckets = new Map();
528
+ const publicIntegrationBuckets = makeBucketRegistry(limits.burst, limits.ratePerMinute);
529
+ // Public delivery never competes with MCP work or another provider route
530
+ // for the same slots. This reserves bounded callback/webhook capacity.
531
+ const publicIntegrationSlotGates = new Map();
467
532
  // Marks requests authenticated via the browser-session COOKIE (bead 7q9).
468
533
  // CSRF enforcement (double-submit) applies only to these — bearer-authed
469
534
  // requests carry no ambient credential a cross-site page could ride.
470
535
  const cookieAuthedReqs = new WeakSet();
471
536
  const slotGate = opts.slotGate ?? makeSlotGate(limits.maxInFlight);
472
537
  // Fire-and-forget: an audit write must never add latency or failure to a
473
- // response. Errors surface once per failure on stderr.
538
+ // response. Errors surface once per failure on stderr. The in-flight appends
539
+ // are tracked so close() can DRAIN them: appendAuthEvent does mkdirSync(
540
+ // .daftari) + appendFile, so an append still pending after shutdown would
541
+ // resurrect .daftari/ under a vault being torn down (ENOTEMPTY during rmSync
542
+ // — observed as a load-sensitive false-red in the ops-floor suite). Draining
543
+ // on close makes shutdown leave no straggler writer.
544
+ const pendingAudits = new Set();
474
545
  const audit = (entry) => {
475
546
  if (!config.server.audit)
476
547
  return;
477
- void appendAuthEvent(vaultRoot, entry).then((r) => {
548
+ const p = appendAuthEvent(vaultRoot, entry).then((r) => {
478
549
  if (!r.ok)
479
550
  process.stderr.write(`daftari serve: auth-log append failed: ${r.error.message}\n`);
480
551
  });
552
+ pendingAudits.add(p);
553
+ void p.finally(() => pendingAudits.delete(p));
481
554
  };
482
555
  // JWKS key set, created lazily on the first OAuth verification: jose
483
556
  // caches fetched keys, so the server stays stateless and offline-tolerant
@@ -640,6 +713,142 @@ export function startHttpServer(vaultRoot, config, tokens, bind, port, opts = {}
640
713
  }
641
714
  }
642
715
  const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
716
+ if (opts.integrationRuntime !== undefined) {
717
+ const handled = await opts.integrationRuntime.handle(req, res, url, {
718
+ admitPublic: (integrationRequest, integrationResponse) => {
719
+ const remote = resolvePublicRemote(integrationRequest.socket.remoteAddress, integrationRequest.headers["x-forwarded-for"], trustedProxies);
720
+ const path = new URL(integrationRequest.url ?? "/", "http://localhost").pathname;
721
+ const take = takeFromRegistry(publicIntegrationBuckets, `${path}\0${remote}`, Date.now());
722
+ if (!take.allowed) {
723
+ integrationResponse.setHeader("Retry-After", String(take.retryAfterSeconds));
724
+ audit({
725
+ outcome: "rate-limited",
726
+ remote,
727
+ method: integrationRequest.method ?? "",
728
+ path,
729
+ });
730
+ writeJson(integrationResponse, 429, {
731
+ error: "rate_limited",
732
+ message: "public integration request rate limit reached",
733
+ });
734
+ return null;
735
+ }
736
+ let publicGate = publicIntegrationSlotGates.get(path);
737
+ if (publicGate === undefined) {
738
+ publicGate = makeSlotGate(Math.max(2, Math.ceil(limits.maxInFlight / 4)));
739
+ publicIntegrationSlotGates.set(path, publicGate);
740
+ }
741
+ if (!tryAcquireSlot(publicGate)) {
742
+ integrationResponse.setHeader("Retry-After", "1");
743
+ audit({
744
+ outcome: "over-capacity",
745
+ remote,
746
+ method: integrationRequest.method ?? "",
747
+ path,
748
+ });
749
+ writeJson(integrationResponse, 503, {
750
+ error: "over_capacity",
751
+ message: "server is at its request ceiling",
752
+ });
753
+ return null;
754
+ }
755
+ audit({
756
+ outcome: "allow",
757
+ remote,
758
+ method: integrationRequest.method ?? "",
759
+ path,
760
+ });
761
+ let released = false;
762
+ return () => {
763
+ if (released)
764
+ return;
765
+ released = true;
766
+ releaseSlot(publicGate);
767
+ };
768
+ },
769
+ authorize: async (integrationRequest, integrationResponse) => {
770
+ const remote = remoteOf(integrationRequest);
771
+ const path = new URL(integrationRequest.url ?? "/", "http://localhost").pathname;
772
+ const penalty = penaltyAllows(penaltyBox, remote, Date.now());
773
+ if (!penalty.allowed) {
774
+ integrationResponse.setHeader("Retry-After", String(penalty.retryAfterSeconds));
775
+ audit({
776
+ outcome: "rate-limited",
777
+ remote,
778
+ method: integrationRequest.method ?? "",
779
+ path,
780
+ });
781
+ writeJson(integrationResponse, 429, {
782
+ error: "rate_limited",
783
+ message: "too many failed authentication attempts",
784
+ });
785
+ return null;
786
+ }
787
+ const access = await authenticate(integrationRequest, integrationResponse, url.pathname);
788
+ if (access === null)
789
+ return null;
790
+ let bucket = principalBuckets.get(access.user);
791
+ if (bucket === undefined) {
792
+ bucket = makeBucket(limits.burst, limits.ratePerMinute, Date.now());
793
+ principalBuckets.set(access.user, bucket);
794
+ }
795
+ const take = tryTake(bucket, Date.now());
796
+ if (!take.allowed) {
797
+ integrationResponse.setHeader("Retry-After", String(take.retryAfterSeconds));
798
+ audit({
799
+ outcome: "rate-limited",
800
+ principal: access.user,
801
+ remote,
802
+ method: integrationRequest.method ?? "",
803
+ path,
804
+ });
805
+ writeJson(integrationResponse, 429, {
806
+ error: "rate_limited",
807
+ message: "per-principal rate limit reached",
808
+ });
809
+ return null;
810
+ }
811
+ if (!tryAcquireSlot(slotGate)) {
812
+ integrationResponse.setHeader("Retry-After", "1");
813
+ audit({
814
+ outcome: "over-capacity",
815
+ principal: access.user,
816
+ remote,
817
+ method: integrationRequest.method ?? "",
818
+ path,
819
+ });
820
+ writeJson(integrationResponse, 503, {
821
+ error: "over_capacity",
822
+ message: "server is at its request ceiling",
823
+ });
824
+ return null;
825
+ }
826
+ let released = false;
827
+ const release = () => {
828
+ if (released)
829
+ return;
830
+ released = true;
831
+ releaseSlot(slotGate);
832
+ };
833
+ integrationResponse.once("finish", release);
834
+ integrationResponse.once("close", release);
835
+ audit({
836
+ outcome: "allow",
837
+ principal: access.user,
838
+ remote,
839
+ method: integrationRequest.method ?? "",
840
+ path,
841
+ });
842
+ return {
843
+ cookieAuthenticated: cookieAuthedReqs.has(integrationRequest),
844
+ canManageIntegrations: canManageIntegrations(access.role),
845
+ };
846
+ },
847
+ checkCsrf,
848
+ });
849
+ if (handled)
850
+ return;
851
+ }
643
852
  // ---------------------------------------------------------------------------
644
853
  // Board login shim (bead 7q9, U5) — mounted BEFORE authenticate(): the login
645
854
  // page and credential POST must be reachable WITHOUT a session. Present only
@@ -992,6 +1201,49 @@ export function startHttpServer(vaultRoot, config, tokens, bind, port, opts = {}
992
1201
  path: "/mcp",
993
1202
  });
994
1203
  try {
1204
+ // Bound the body BEFORE the MCP adapter reads it. toWebRequest buffers
1205
+ // the whole stream unbounded, so a large or slow body would pin memory /
1206
+ // this in-flight slot. We pre-read under the byte cap + deadline and hand
1207
+ // the parsed value to the adapter as `parsedBody` — the documented
1208
+ // pass-through that makes it read nothing from `req`.
1209
+ let parsedBody;
1210
+ if (req.method === "POST" || req.method === "PUT" || req.method === "PATCH") {
1211
+ const read = await readBodyBounded(req, limits.maxBodyBytes, MCP_BODY_DEADLINE_MS);
1212
+ if (read.status !== "ok") {
1213
+ // The body was never fully read, so close the connection rather than
1214
+ // try to reuse it — the unread remainder would corrupt keep-alive.
1215
+ res.setHeader("Connection", "close");
1216
+ if (read.status === "too_large") {
1217
+ writeJson(res, 413, {
1218
+ error: "payload_too_large",
1219
+ message: "request body exceeds the configured limit",
1220
+ });
1221
+ }
1222
+ else if (read.status === "timeout") {
1223
+ writeJson(res, 408, {
1224
+ error: "request_timeout",
1225
+ message: "request body was not received in time",
1226
+ });
1227
+ }
1228
+ else {
1229
+ writeJson(res, 400, { error: "bad_request", message: "could not read request body" });
1230
+ }
1231
+ return;
1232
+ }
1233
+ if (read.body.length > 0) {
1234
+ try {
1235
+ parsedBody = JSON.parse(read.body);
1236
+ }
1237
+ catch {
1238
+ writeJson(res, 400, {
1239
+ jsonrpc: "2.0",
1240
+ id: null,
1241
+ error: { code: -32700, message: "Parse error" },
1242
+ });
1243
+ return;
1244
+ }
1245
+ }
1246
+ }
995
1247
  // toNodeHandler forwards req.auth as the handler's pass-through authInfo
996
1248
  // (it performs no verification of its own — ours ran above). The bearer
997
1249
  // is the credential; `_meta` client info is diagnostics, never identity.
@@ -1001,7 +1253,7 @@ export function startHttpServer(vaultRoot, config, tokens, bind, port, opts = {}
1001
1253
  scopes: [],
1002
1254
  extra: { access },
1003
1255
  };
1004
- await nodeHandler(req, res);
1256
+ await nodeHandler(req, res, parsedBody);
1005
1257
  }
1006
1258
  finally {
1007
1259
  releaseSlot(slotGate);
@@ -1010,20 +1262,42 @@ export function startHttpServer(vaultRoot, config, tokens, bind, port, opts = {}
1010
1262
  return new Promise((resolveStart, rejectStart) => {
1011
1263
  httpServer.once("error", rejectStart);
1012
1264
  httpServer.listen(port, bind, () => {
1013
- httpServer.removeListener("error", rejectStart);
1014
1265
  const address = httpServer.address();
1015
1266
  const boundPort = typeof address === "object" && address !== null ? address.port : port;
1016
1267
  if (isLoopbackBind(bind))
1017
1268
  loopbackGuard = makeLoopbackGuard(boundPort);
1018
- resolveStart({
1019
- port: boundPort,
1020
- close: async () => {
1021
- // close() aborts in-flight exchanges and resolves once every
1022
- // per-request instance has terminated — there is no session table
1023
- // to drain.
1024
- await mcpHandler.close();
1025
- await new Promise((r) => httpServer.close(() => r()));
1026
- },
1269
+ void (async () => {
1270
+ if (opts.integrationRuntime !== undefined) {
1271
+ const started = await opts.integrationRuntime.start(httpCallbackBase(bind, boundPort));
1272
+ if (!started.ok) {
1273
+ await new Promise((done) => httpServer.close(() => done()));
1274
+ rejectStart(started.error);
1275
+ return;
1276
+ }
1277
+ }
1278
+ httpServer.removeListener("error", rejectStart);
1279
+ resolveStart({
1280
+ port: boundPort,
1281
+ close: async () => {
1282
+ // Freeze network admission first so a webhook stream cannot keep
1283
+ // requesting connector reruns while shutdown drains the active
1284
+ // cycle. The callback resolves after existing HTTP exchanges end.
1285
+ const listenerClosed = new Promise((done) => httpServer.close(() => done()));
1286
+ await opts.integrationRuntime?.close();
1287
+ // close() aborts in-flight exchanges and resolves once every
1288
+ // per-request instance has terminated — there is no session table
1289
+ // to drain.
1290
+ await mcpHandler.close();
1291
+ await listenerClosed;
1292
+ // Drain fire-and-forget audit appends last: no request can start a
1293
+ // new one once the listener is closed, so this settles the tail and
1294
+ // guarantees no append resurrects .daftari/ after shutdown.
1295
+ await Promise.allSettled([...pendingAudits]);
1296
+ },
1297
+ });
1298
+ })().catch(async (cause) => {
1299
+ await new Promise((done) => httpServer.close(() => done()));
1300
+ rejectStart(cause);
1027
1301
  });
1028
1302
  });
1029
1303
  });
@@ -1096,6 +1370,23 @@ export async function runServe(argv) {
1096
1370
  process.stderr.write(`daftari serve: ${gate.error}\n`);
1097
1371
  return 2;
1098
1372
  }
1373
+ let integrationRuntime;
1374
+ if (config.value.integrations !== undefined) {
1375
+ const created = createConfiguredIntegrationRuntime({
1376
+ vaultRoot,
1377
+ config: config.value.integrations,
1378
+ environment: process.env,
1379
+ ...(config.value.server.publicBaseUrl === undefined
1380
+ ? {}
1381
+ : { publicBaseUrl: config.value.server.publicBaseUrl }),
1382
+ onError: (message) => process.stderr.write(`daftari: warning: ${message}\n`),
1383
+ });
1384
+ if (!created.ok) {
1385
+ process.stderr.write(`daftari serve: ${created.error.message}\n`);
1386
+ return 2;
1387
+ }
1388
+ integrationRuntime = created.value;
1389
+ }
1099
1390
  // Storage backing for periodic sync (#6): created and validated BEFORE the
1100
1391
  // lock and the listener. A config-declared capability that cannot run must
1101
1392
  // refuse at startup — returning an exit code after the listener is up
@@ -1118,10 +1409,24 @@ export async function runServe(argv) {
1118
1409
  // a failure between here and the listener opening must still release the
1119
1410
  // lock on exit. `handle` is assigned once the listener is up.
1120
1411
  let handle = null;
1121
- installShutdownHandlers(vaultRoot, () => {
1412
+ installShutdownHandlers(vaultRoot, async () => {
1122
1413
  if (handle)
1123
- void handle.close();
1414
+ await handle.close();
1124
1415
  });
1416
+ // Existing vaults may predate the integration-state ignore block. Upgrade
1417
+ // it only after this process owns the vault: a refused second process must
1418
+ // remain read-only.
1419
+ if (config.value.integrations !== undefined) {
1420
+ try {
1421
+ await ensureVaultGitignore(vaultRoot);
1422
+ }
1423
+ catch (cause) {
1424
+ releaseLock(vaultRoot);
1425
+ const reason = cause instanceof Error ? cause.message : String(cause);
1426
+ process.stderr.write(`daftari serve: cannot secure local integration state: ${reason}\n`);
1427
+ return 3;
1428
+ }
1429
+ }
1125
1430
  try {
1126
1431
  setProvider(config.value.embeddingProvider);
1127
1432
  }
@@ -1135,6 +1440,7 @@ export async function runServe(argv) {
1135
1440
  setVecKnnK(config.value.search.vecKnnK);
1136
1441
  setDefaultWeights(config.value.search.weights);
1137
1442
  setSuppressSuperseded(config.value.search.suppressSuperseded);
1443
+ setGraphExpandConfig(config.value.search.graphExpand);
1138
1444
  // Startup warnings mirror stdio's: unknown tool names in the tools block.
1139
1445
  for (const name of resolveToolExposure(config.value.tools).unknown) {
1140
1446
  process.stderr.write(`daftari: warning: tools.include/exclude names unknown tool '${name}' — ignored\n`);
@@ -1143,6 +1449,7 @@ export async function runServe(argv) {
1143
1449
  handle = await startHttpServer(vaultRoot, config.value, gate.tokens, bind, port, {
1144
1450
  legacyHttp,
1145
1451
  session: gate.session,
1452
+ ...(integrationRuntime === undefined ? {} : { integrationRuntime }),
1146
1453
  });
1147
1454
  }
1148
1455
  catch (e) {