@remnic/core 10.0.187 → 10.0.189
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/access-admin-ops-surface.js +39 -39
- package/dist/access-cli.js +39 -39
- package/dist/access-extraction-force-flush-preempt.js +39 -39
- package/dist/access-extraction-force-flush.d.ts.map +1 -1
- package/dist/access-extraction-force-flush.js +39 -39
- package/dist/access-http-briefing.js +39 -39
- package/dist/access-http-query.js +39 -39
- package/dist/access-http.js +39 -39
- package/dist/access-identity-continuity-surface.js +39 -39
- package/dist/access-lcm-surface.js +39 -39
- package/dist/access-mcp.js +39 -39
- package/dist/access-observe-helpers.d.ts +1 -4
- package/dist/access-observe-helpers.d.ts.map +1 -1
- package/dist/access-observe-helpers.js +1 -1
- package/dist/access-observe-write-surface.d.ts +0 -4
- package/dist/access-observe-write-surface.d.ts.map +1 -1
- package/dist/access-observe-write-surface.js +39 -39
- package/dist/access-operations-batch.js +39 -39
- package/dist/access-operations.js +39 -39
- package/dist/access-recall-concurrency.js +39 -39
- package/dist/access-recall-response.js +39 -39
- package/dist/access-recall-surface.js +39 -39
- package/dist/access-service.d.ts +0 -2
- package/dist/access-service.d.ts.map +1 -1
- package/dist/access-service.js +39 -39
- package/dist/active-recall.js +1 -1
- package/dist/briefing.js +2 -2
- package/dist/buffer-add-turns-bounded.js +2 -2
- package/dist/buffer-admission-idempotency.d.ts.map +1 -1
- package/dist/buffer-admission-idempotency.js +1 -1
- package/dist/buffer-persistence.d.ts +14 -0
- package/dist/buffer-persistence.d.ts.map +1 -1
- package/dist/buffer-persistence.js +3 -1
- package/dist/buffer.js +4 -4
- package/dist/calibration.js +3 -3
- package/dist/causal-consolidation.js +3 -3
- package/dist/{chunk-2ZS7CB5H.js → chunk-2I3ETW3G.js} +2 -2
- package/dist/{chunk-PSUSA5SM.js → chunk-2VNMSEZR.js} +2 -2
- package/dist/{chunk-LANV6I63.js → chunk-56BNHKZM.js} +7 -7
- package/dist/{chunk-3FEQNKMB.js → chunk-5WDUY2ZZ.js} +2 -2
- package/dist/{chunk-BANMPRSC.js → chunk-65DWWJKI.js} +2 -2
- package/dist/{chunk-O5ZCDK32.js → chunk-6AMFGXG2.js} +39 -11
- package/dist/chunk-6AMFGXG2.js.map +1 -0
- package/dist/{chunk-VZBKCRU7.js → chunk-6MDHK6NO.js} +2 -2
- package/dist/{chunk-2RAVHJCD.js → chunk-6OHCNTKU.js} +2 -2
- package/dist/{chunk-P5FF44G3.js → chunk-7X6EZ2QB.js} +4 -4
- package/dist/{chunk-K5RGHUX7.js → chunk-A32EBVCZ.js} +2 -2
- package/dist/{chunk-L2DYRQQH.js → chunk-ABPOW3SX.js} +73 -103
- package/dist/chunk-ABPOW3SX.js.map +1 -0
- package/dist/{chunk-KVM3BS5K.js → chunk-ATPY63F5.js} +2 -2
- package/dist/chunk-B256MMS7.js +205 -0
- package/dist/chunk-B256MMS7.js.map +1 -0
- package/dist/{chunk-MOWLANEJ.js → chunk-C6Y2YHJL.js} +2 -2
- package/dist/{chunk-IMT7NTFV.js → chunk-CIAQK2OU.js} +6 -1
- package/dist/{chunk-IMT7NTFV.js.map → chunk-CIAQK2OU.js.map} +1 -1
- package/dist/{chunk-AJVCM2E3.js → chunk-CYX7JI4O.js} +2 -2
- package/dist/{chunk-76KL37ON.js → chunk-D3FP4JQI.js} +2 -2
- package/dist/{chunk-TQXKUQFX.js → chunk-FZU62GIV.js} +7 -6
- package/dist/chunk-FZU62GIV.js.map +1 -0
- package/dist/{chunk-YDGQYG4L.js → chunk-H442UKDV.js} +8 -8
- package/dist/{chunk-FXDQO7VX.js → chunk-HL53XYHC.js} +3 -3
- package/dist/{chunk-4VFEN3WT.js → chunk-IMSLPQJB.js} +2 -2
- package/dist/{chunk-FKLSIQBF.js → chunk-K3JGILZC.js} +3 -3
- package/dist/{chunk-JRHOL6B5.js → chunk-K6BS5RB4.js} +2 -2
- package/dist/{chunk-HHCLV572.js → chunk-KVTUUU7H.js} +2 -2
- package/dist/{chunk-TRFSTCQK.js → chunk-LL6WEMTC.js} +34 -6
- package/dist/chunk-LL6WEMTC.js.map +1 -0
- package/dist/{chunk-2WITFW6F.js → chunk-NKHRIDYP.js} +2 -2
- package/dist/{chunk-KYZMCH4I.js → chunk-NN3LMWKA.js} +12 -4
- package/dist/chunk-NN3LMWKA.js.map +1 -0
- package/dist/{chunk-CYTTY4M2.js → chunk-OPXK5IQU.js} +47 -47
- package/dist/chunk-OPXK5IQU.js.map +1 -0
- package/dist/{chunk-CTWWMEGR.js → chunk-PCGBESAN.js} +2 -2
- package/dist/{chunk-MDXZIU5X.js → chunk-QGT5KNQT.js} +2 -2
- package/dist/{chunk-YJZDRM33.js → chunk-QXHWOUS6.js} +3 -3
- package/dist/{chunk-OVJAQWHO.js → chunk-RPOG6HLI.js} +2 -2
- package/dist/{chunk-2QQCCUXF.js → chunk-RVODERA5.js} +2 -2
- package/dist/{chunk-FQE5QN5Q.js → chunk-TAPF7T26.js} +2 -2
- package/dist/{chunk-7UTCETGK.js → chunk-TQODBIMW.js} +2 -2
- package/dist/{chunk-KZJEKKK6.js → chunk-VQQZJ6KJ.js} +3 -3
- package/dist/{chunk-I233SAGS.js → chunk-X3ERYOLO.js} +2 -2
- package/dist/{chunk-UCCXPMTP.js → chunk-XQF2KLV5.js} +2 -2
- package/dist/{chunk-LPYGM6RE.js → chunk-XWPYK5T4.js} +2 -2
- package/dist/{chunk-7EPEZYX3.js → chunk-YVXR7CPS.js} +2 -2
- package/dist/cli.js +39 -39
- package/dist/coding/export-okf-codegraph.js +1 -1
- package/dist/compounding/engine.js +3 -3
- package/dist/config.js +1 -1
- package/dist/connectors/codex-materialize-runner.js +2 -2
- package/dist/connectors/index.js +2 -2
- package/dist/convergence-refresh.js +4 -4
- package/dist/corpus-watermark.js +4 -4
- package/dist/entity-retrieval.js +2 -2
- package/dist/explicit-capture.js +39 -39
- package/dist/extraction.js +6 -6
- package/dist/harmonic-retrieval.js +2 -2
- package/dist/index.js +39 -39
- package/dist/local-llm-helpers.d.ts.map +1 -1
- package/dist/local-llm-helpers.js +2 -2
- package/dist/local-llm-transport.d.ts +82 -9
- package/dist/local-llm-transport.d.ts.map +1 -1
- package/dist/local-llm-transport.js +11 -3
- package/dist/local-llm.d.ts.map +1 -1
- package/dist/local-llm.js +3 -3
- package/dist/location/index.js +3 -3
- package/dist/maintenance/memory-governance.js +2 -2
- package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +2 -2
- package/dist/maintenance/rebuild-memory-projection-runner.js +4 -4
- package/dist/maintenance/rebuild-memory-projection.js +3 -3
- package/dist/namespaces/migrate.js +3 -3
- package/dist/namespaces/storage.js +2 -2
- package/dist/operator-doctor-corpus.js +5 -5
- package/dist/operator-toolkit.js +8 -8
- package/dist/orchestration/backlog-scheduler.js +40 -40
- package/dist/orchestration/buffer-drain-runner.js +40 -40
- package/dist/orchestration/extraction-queue-coordinator.d.ts +14 -1
- package/dist/orchestration/extraction-queue-coordinator.d.ts.map +1 -1
- package/dist/orchestration/maintenance.js +13 -13
- package/dist/orchestrator.js +39 -39
- package/dist/recall-navigation.js +39 -39
- package/dist/resume-bundles.js +1 -1
- package/dist/semantic-consolidation.js +3 -3
- package/dist/semantic-rule-promotion.js +2 -2
- package/dist/semantic-rule-verifier.js +2 -2
- package/dist/source-agent-qualifier.js +2 -2
- package/dist/standup/build.js +2 -2
- package/dist/storage/entity-canonical-id-migration-runner.d.ts.map +1 -1
- package/dist/storage.js +1 -1
- package/dist/summarizer.js +4 -4
- package/dist/support-passport/index.js +4 -4
- package/dist/transfer/export-okf.js +2 -2
- package/dist/verified-recall.js +2 -2
- package/dist/who-knows-cli.js +3 -3
- package/dist/who-knows.js +2 -2
- package/package.json +2 -2
- package/src/access-extraction-force-flush-preempt-coordinator.test.ts +71 -0
- package/src/access-extraction-force-flush.ts +20 -56
- package/src/access-observe-helpers.test.ts +71 -88
- package/src/access-observe-helpers.ts +60 -73
- package/src/access-observe-write-surface.ts +1 -22
- package/src/access-service-observe-lcm-parity.test.ts +176 -89
- package/src/access-service-observe-scope.test.ts +18 -86
- package/src/access-service.ts +0 -13
- package/src/buffer-admission-idempotency.test.ts +115 -0
- package/src/buffer-admission-idempotency.ts +67 -11
- package/src/buffer-persistence.ts +46 -5
- package/src/local-llm-headers-timeout.test.ts +246 -16
- package/src/local-llm-helpers.ts +14 -4
- package/src/local-llm-transport.ts +253 -16
- package/src/local-llm.ts +7 -8
- package/src/orchestration/extraction-queue-coordinator.ts +34 -3
- package/src/storage/entity-canonical-id-migration-runner.test.ts +90 -0
- package/src/storage/entity-canonical-id-migration-runner.ts +10 -0
- package/dist/chunk-5YQXWVCP.js +0 -74
- package/dist/chunk-5YQXWVCP.js.map +0 -1
- package/dist/chunk-CYTTY4M2.js.map +0 -1
- package/dist/chunk-KYZMCH4I.js.map +0 -1
- package/dist/chunk-L2DYRQQH.js.map +0 -1
- package/dist/chunk-O5ZCDK32.js.map +0 -1
- package/dist/chunk-TQXKUQFX.js.map +0 -1
- package/dist/chunk-TRFSTCQK.js.map +0 -1
- /package/dist/{chunk-2ZS7CB5H.js.map → chunk-2I3ETW3G.js.map} +0 -0
- /package/dist/{chunk-PSUSA5SM.js.map → chunk-2VNMSEZR.js.map} +0 -0
- /package/dist/{chunk-LANV6I63.js.map → chunk-56BNHKZM.js.map} +0 -0
- /package/dist/{chunk-3FEQNKMB.js.map → chunk-5WDUY2ZZ.js.map} +0 -0
- /package/dist/{chunk-BANMPRSC.js.map → chunk-65DWWJKI.js.map} +0 -0
- /package/dist/{chunk-VZBKCRU7.js.map → chunk-6MDHK6NO.js.map} +0 -0
- /package/dist/{chunk-2RAVHJCD.js.map → chunk-6OHCNTKU.js.map} +0 -0
- /package/dist/{chunk-P5FF44G3.js.map → chunk-7X6EZ2QB.js.map} +0 -0
- /package/dist/{chunk-K5RGHUX7.js.map → chunk-A32EBVCZ.js.map} +0 -0
- /package/dist/{chunk-KVM3BS5K.js.map → chunk-ATPY63F5.js.map} +0 -0
- /package/dist/{chunk-MOWLANEJ.js.map → chunk-C6Y2YHJL.js.map} +0 -0
- /package/dist/{chunk-AJVCM2E3.js.map → chunk-CYX7JI4O.js.map} +0 -0
- /package/dist/{chunk-76KL37ON.js.map → chunk-D3FP4JQI.js.map} +0 -0
- /package/dist/{chunk-YDGQYG4L.js.map → chunk-H442UKDV.js.map} +0 -0
- /package/dist/{chunk-FXDQO7VX.js.map → chunk-HL53XYHC.js.map} +0 -0
- /package/dist/{chunk-4VFEN3WT.js.map → chunk-IMSLPQJB.js.map} +0 -0
- /package/dist/{chunk-FKLSIQBF.js.map → chunk-K3JGILZC.js.map} +0 -0
- /package/dist/{chunk-JRHOL6B5.js.map → chunk-K6BS5RB4.js.map} +0 -0
- /package/dist/{chunk-HHCLV572.js.map → chunk-KVTUUU7H.js.map} +0 -0
- /package/dist/{chunk-2WITFW6F.js.map → chunk-NKHRIDYP.js.map} +0 -0
- /package/dist/{chunk-CTWWMEGR.js.map → chunk-PCGBESAN.js.map} +0 -0
- /package/dist/{chunk-MDXZIU5X.js.map → chunk-QGT5KNQT.js.map} +0 -0
- /package/dist/{chunk-YJZDRM33.js.map → chunk-QXHWOUS6.js.map} +0 -0
- /package/dist/{chunk-OVJAQWHO.js.map → chunk-RPOG6HLI.js.map} +0 -0
- /package/dist/{chunk-2QQCCUXF.js.map → chunk-RVODERA5.js.map} +0 -0
- /package/dist/{chunk-FQE5QN5Q.js.map → chunk-TAPF7T26.js.map} +0 -0
- /package/dist/{chunk-7UTCETGK.js.map → chunk-TQODBIMW.js.map} +0 -0
- /package/dist/{chunk-KZJEKKK6.js.map → chunk-VQQZJ6KJ.js.map} +0 -0
- /package/dist/{chunk-I233SAGS.js.map → chunk-X3ERYOLO.js.map} +0 -0
- /package/dist/{chunk-UCCXPMTP.js.map → chunk-XQF2KLV5.js.map} +0 -0
- /package/dist/{chunk-LPYGM6RE.js.map → chunk-XWPYK5T4.js.map} +0 -0
- /package/dist/{chunk-7EPEZYX3.js.map → chunk-YVXR7CPS.js.map} +0 -0
|
@@ -5,6 +5,7 @@ import test from "node:test";
|
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
Agent,
|
|
8
|
+
EnvHttpProxyAgent,
|
|
8
9
|
ProxyAgent,
|
|
9
10
|
fetch as undiciFetch,
|
|
10
11
|
getGlobalDispatcher,
|
|
@@ -14,7 +15,7 @@ import {
|
|
|
14
15
|
import { LocalLlmClient } from "./local-llm.js";
|
|
15
16
|
import { probeFetch, type LocalLlmChatCompletionOptions } from "./local-llm-helpers.js";
|
|
16
17
|
import { classifyAnalysisProviderError } from "./activity/timeline/analysis-provider.js";
|
|
17
|
-
import { ChatTransport } from "./local-llm-transport.js";
|
|
18
|
+
import { ChatTransport, EXTRACTION_MIN_TRANSPORT_BUDGET_MS, isInvalidDispatcherArg, resolveProxyEnv, routeBudgets } from "./local-llm-transport.js";
|
|
18
19
|
import { initLogger, resetLogger } from "./logger.js";
|
|
19
20
|
import type { PluginConfig } from "./types.js";
|
|
20
21
|
|
|
@@ -82,9 +83,11 @@ function dispatcherOf(client: LocalLlmClient): Agent {
|
|
|
82
83
|
chatTransport: ChatTransport;
|
|
83
84
|
config: PluginConfig;
|
|
84
85
|
};
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const dispatcher = seam.chatTransport.dispatcherFor(seam.config.localLlmTimeoutMs);
|
|
87
|
+
// The direct/plain path returns the widened Agent; the proxyline chain
|
|
88
|
+
// returns a ProxyAgent, which its own test covers.
|
|
89
|
+
assert.ok(dispatcher instanceof Agent, "expected a widened pool under the default global dispatcher");
|
|
90
|
+
return dispatcher;
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
function undiciErrorCode(err: unknown): string | undefined {
|
|
@@ -467,27 +470,41 @@ test("a truncated 503 body retains the configured retry", async () => {
|
|
|
467
470
|
test("a process-wide custom dispatcher is left in place", async () => {
|
|
468
471
|
// A deployment may reach the local endpoint only through a ProxyAgent (or
|
|
469
472
|
// another custom connect/TLS/DNS transport) installed with
|
|
470
|
-
// setGlobalDispatcher.
|
|
471
|
-
// the
|
|
473
|
+
// setGlobalDispatcher. When no standard proxy environment variable names
|
|
474
|
+
// that route, the transport cannot mirror it: swapping in our own pool
|
|
475
|
+
// would bypass it and break the request outright, so the global
|
|
476
|
+
// dispatcher must stand down untouched.
|
|
472
477
|
const original = getGlobalDispatcher();
|
|
473
478
|
const proxy = new ProxyAgent("http://127.0.0.1:9");
|
|
479
|
+
const prior = {
|
|
480
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY,
|
|
481
|
+
https_proxy: process.env.https_proxy,
|
|
482
|
+
HTTP_PROXY: process.env.HTTP_PROXY,
|
|
483
|
+
http_proxy: process.env.http_proxy,
|
|
484
|
+
REMNIC_LOCAL_LLM_DIRECT: process.env.REMNIC_LOCAL_LLM_DIRECT,
|
|
485
|
+
};
|
|
474
486
|
try {
|
|
487
|
+
for (const key of Object.keys(prior)) delete process.env[key];
|
|
475
488
|
setGlobalDispatcher(proxy);
|
|
476
489
|
const transport = new ChatTransport();
|
|
477
490
|
assert.equal(
|
|
478
491
|
transport.dispatcherFor(900_000),
|
|
479
492
|
undefined,
|
|
480
|
-
"
|
|
493
|
+
"no named proxy environment: the transport cannot mirror the custom dispatcher",
|
|
494
|
+
);
|
|
495
|
+
assert.equal(
|
|
496
|
+
getGlobalDispatcher(),
|
|
497
|
+
proxy,
|
|
498
|
+
"must not displace a non-Agent global dispatcher it cannot mirror",
|
|
481
499
|
);
|
|
482
500
|
} finally {
|
|
501
|
+
for (const [key, value] of Object.entries(prior)) {
|
|
502
|
+
if (value !== undefined) process.env[key] = value;
|
|
503
|
+
else delete process.env[key];
|
|
504
|
+
}
|
|
483
505
|
setGlobalDispatcher(original);
|
|
484
506
|
await proxy.close();
|
|
485
507
|
}
|
|
486
|
-
|
|
487
|
-
// ...and the widened pool returns once the default transport is back.
|
|
488
|
-
const restored = new ChatTransport().dispatcherFor(900_000);
|
|
489
|
-
assert.ok(restored);
|
|
490
|
-
await restored.close();
|
|
491
508
|
});
|
|
492
509
|
|
|
493
510
|
test("an explicit opt-in widens the pool even when a custom global dispatcher is installed", async () => {
|
|
@@ -499,10 +516,16 @@ test("an explicit opt-in widens the pool even when a custom global dispatcher is
|
|
|
499
516
|
// previous behavior (leave the custom dispatcher alone) is unchanged.
|
|
500
517
|
const original = getGlobalDispatcher();
|
|
501
518
|
const proxy = new ProxyAgent("http://127.0.0.1:9");
|
|
502
|
-
const
|
|
519
|
+
const prior = {
|
|
520
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY,
|
|
521
|
+
https_proxy: process.env.https_proxy,
|
|
522
|
+
HTTP_PROXY: process.env.HTTP_PROXY,
|
|
523
|
+
http_proxy: process.env.http_proxy,
|
|
524
|
+
REMNIC_LOCAL_LLM_DIRECT: process.env.REMNIC_LOCAL_LLM_DIRECT,
|
|
525
|
+
};
|
|
503
526
|
try {
|
|
527
|
+
for (const key of Object.keys(prior)) delete process.env[key];
|
|
504
528
|
setGlobalDispatcher(proxy);
|
|
505
|
-
delete process.env.REMNIC_LOCAL_LLM_DIRECT;
|
|
506
529
|
assert.equal(new ChatTransport().dispatcherFor(900_000), undefined, "no flag: custom dispatcher stays");
|
|
507
530
|
|
|
508
531
|
process.env.REMNIC_LOCAL_LLM_DIRECT = "1";
|
|
@@ -515,13 +538,60 @@ test("an explicit opt-in widens the pool even when a custom global dispatcher is
|
|
|
515
538
|
process.env.REMNIC_LOCAL_LLM_DIRECT = "0";
|
|
516
539
|
assert.equal(new ChatTransport().dispatcherFor(900_000), undefined, "flag 0: custom dispatcher stays");
|
|
517
540
|
} finally {
|
|
518
|
-
|
|
519
|
-
|
|
541
|
+
for (const [key, value] of Object.entries(prior)) {
|
|
542
|
+
if (value !== undefined) process.env[key] = value;
|
|
543
|
+
else delete process.env[key];
|
|
544
|
+
}
|
|
520
545
|
setGlobalDispatcher(original);
|
|
521
546
|
await proxy.close();
|
|
522
547
|
}
|
|
523
548
|
});
|
|
524
549
|
|
|
550
|
+
test("a request the runtime rejects with UND_ERR_INVALID_ARG falls back to the bundled undici fetch", async () => {
|
|
551
|
+
// Node 26 rejects a per-request dispatcher in global fetch's init with
|
|
552
|
+
// UND_ERR_INVALID_ARG, wrapped in TypeError("fetch failed"). The transport
|
|
553
|
+
// must retry through the bundled undici's fetch — which accepts the same
|
|
554
|
+
// init — rather than dying on the widened-budget path.
|
|
555
|
+
const server = http.createServer((_req, res) => {
|
|
556
|
+
res.setHeader("content-type", "application/json");
|
|
557
|
+
res.end('{"choices":[{"message":{"content":"the answer"}}]}');
|
|
558
|
+
});
|
|
559
|
+
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
560
|
+
// SAFETY: listen(0, "127.0.0.1") above binds a TCP port, so address() is an AddressInfo, never a pipe string or null.
|
|
561
|
+
const { port } = server.address() as AddressInfo;
|
|
562
|
+
const original = globalThis.fetch;
|
|
563
|
+
const globalCalls: string[] = [];
|
|
564
|
+
try {
|
|
565
|
+
const fakeFetch: typeof fetch = async (url) => {
|
|
566
|
+
globalCalls.push(String(url));
|
|
567
|
+
throw Object.assign(new TypeError("fetch failed"), { code: "UND_ERR_INVALID_ARG" });
|
|
568
|
+
};
|
|
569
|
+
globalThis.fetch = fakeFetch;
|
|
570
|
+
const transport = new ChatTransport();
|
|
571
|
+
const response = await transport.post({
|
|
572
|
+
url: `http://127.0.0.1:${port}/v1/chat/completions`,
|
|
573
|
+
budgetMs: 60_000,
|
|
574
|
+
headers: { "content-type": "application/json" },
|
|
575
|
+
body: "{}",
|
|
576
|
+
signal: AbortSignal.timeout(30_000),
|
|
577
|
+
});
|
|
578
|
+
assert.equal(response.status, 200, "the undici fallback must serve the request");
|
|
579
|
+
assert.ok(globalCalls.length >= 1, "the global fetch was attempted first");
|
|
580
|
+
} finally {
|
|
581
|
+
globalThis.fetch = original;
|
|
582
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
test("isInvalidDispatcherArg finds the code on the error and down its cause chain", () => {
|
|
587
|
+
const direct = Object.assign(new TypeError("fetch failed"), { code: "UND_ERR_INVALID_ARG" });
|
|
588
|
+
assert.equal(isInvalidDispatcherArg(direct), true);
|
|
589
|
+
const wrapped = new TypeError("fetch failed", { cause: direct });
|
|
590
|
+
assert.equal(isInvalidDispatcherArg(wrapped), true);
|
|
591
|
+
const plain = Object.assign(new TypeError("fetch failed"), { code: "ECONNREFUSED" });
|
|
592
|
+
assert.equal(isInvalidDispatcherArg(plain), false);
|
|
593
|
+
});
|
|
594
|
+
|
|
525
595
|
test("REMNIC_LOCAL_LLM_DIRECT=1 also sends availability probes around a global proxy dispatcher", async () => {
|
|
526
596
|
// A host whose global dispatcher cannot reach the LAN model server would fail
|
|
527
597
|
// checkAvailability() before any chat request runs, so direct mode must cover
|
|
@@ -670,3 +740,163 @@ test("failureDiag preserves a 429 rate-limit failure cause (issue #2891)", { con
|
|
|
670
740
|
await dispatcherOf(client).close();
|
|
671
741
|
}
|
|
672
742
|
});
|
|
743
|
+
|
|
744
|
+
test("the proxyline dispatcher is chained with the widened budgets when the proxy environment names it", async () => {
|
|
745
|
+
// Issue #2148 root cause: an agent runtime's managed proxy dispatcher
|
|
746
|
+
// ("proxyline") kept undici's 300s headersTimeout, so extraction
|
|
747
|
+
// calls over five minutes died. When the standard egress-proxy variables
|
|
748
|
+
// name the route, dispatcherFor chains a ProxyAgent over the SAME proxy
|
|
749
|
+
// with the route's budgets instead of leaving the cap in place.
|
|
750
|
+
const original = getGlobalDispatcher();
|
|
751
|
+
const proxy = new ProxyAgent("http://127.0.0.1:9");
|
|
752
|
+
const prior = {
|
|
753
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY,
|
|
754
|
+
https_proxy: process.env.https_proxy,
|
|
755
|
+
HTTP_PROXY: process.env.HTTP_PROXY,
|
|
756
|
+
http_proxy: process.env.http_proxy,
|
|
757
|
+
REMNIC_LOCAL_LLM_DIRECT: process.env.REMNIC_LOCAL_LLM_DIRECT,
|
|
758
|
+
};
|
|
759
|
+
try {
|
|
760
|
+
setGlobalDispatcher(proxy);
|
|
761
|
+
delete process.env.https_proxy;
|
|
762
|
+
delete process.env.HTTP_PROXY;
|
|
763
|
+
delete process.env.http_proxy;
|
|
764
|
+
delete process.env.REMNIC_LOCAL_LLM_DIRECT;
|
|
765
|
+
process.env.HTTPS_PROXY = "http://127.0.0.1:9";
|
|
766
|
+
const transport = new ChatTransport();
|
|
767
|
+
const chained = transport.dispatcherFor(900_000);
|
|
768
|
+
assert.ok(
|
|
769
|
+
chained instanceof EnvHttpProxyAgent,
|
|
770
|
+
"a named proxy environment must produce a chained EnvHttpProxyAgent, not undefined",
|
|
771
|
+
);
|
|
772
|
+
const again = transport.dispatcherFor(900_000);
|
|
773
|
+
assert.equal(again, chained, "the chained proxy pool is cached per proxy+budget");
|
|
774
|
+
await chained.close();
|
|
775
|
+
} finally {
|
|
776
|
+
for (const [key, value] of Object.entries(prior)) {
|
|
777
|
+
if (value !== undefined) process.env[key] = value;
|
|
778
|
+
else delete process.env[key];
|
|
779
|
+
}
|
|
780
|
+
setGlobalDispatcher(original);
|
|
781
|
+
await proxy.close();
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
test("NO_PROXY on the request URL keeps the installed custom dispatcher instead of replacing it", async () => {
|
|
786
|
+
// A host may install a global dispatcher whose custom DNS or connect logic
|
|
787
|
+
// the local endpoint needs (for example model.internal resolves only
|
|
788
|
+
// through it). With NO_PROXY covering that host, the environment routes
|
|
789
|
+
// the request direct — but a fresh direct pool cannot reproduce the
|
|
790
|
+
// custom transport, so dispatcherFor must leave the installed dispatcher
|
|
791
|
+
// in place rather than swapping in the chained EnvHttpProxyAgent either.
|
|
792
|
+
const original = getGlobalDispatcher();
|
|
793
|
+
const proxy = new ProxyAgent("http://127.0.0.1:9");
|
|
794
|
+
const prior = {
|
|
795
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY,
|
|
796
|
+
https_proxy: process.env.https_proxy,
|
|
797
|
+
HTTP_PROXY: process.env.HTTP_PROXY,
|
|
798
|
+
http_proxy: process.env.http_proxy,
|
|
799
|
+
NO_PROXY: process.env.NO_PROXY,
|
|
800
|
+
no_proxy: process.env.no_proxy,
|
|
801
|
+
REMNIC_LOCAL_LLM_DIRECT: process.env.REMNIC_LOCAL_LLM_DIRECT,
|
|
802
|
+
};
|
|
803
|
+
try {
|
|
804
|
+
setGlobalDispatcher(proxy);
|
|
805
|
+
delete process.env.https_proxy;
|
|
806
|
+
delete process.env.HTTP_PROXY;
|
|
807
|
+
delete process.env.http_proxy;
|
|
808
|
+
delete process.env.no_proxy;
|
|
809
|
+
delete process.env.REMNIC_LOCAL_LLM_DIRECT;
|
|
810
|
+
process.env.HTTPS_PROXY = "http://127.0.0.1:9";
|
|
811
|
+
process.env.NO_PROXY = "model.internal:80,.lan.example";
|
|
812
|
+
|
|
813
|
+
const transport = new ChatTransport();
|
|
814
|
+
const exempt = transport.dispatcherFor(900_000, "http://model.internal/v1/chat/completions");
|
|
815
|
+
assert.equal(exempt, undefined, "host:80 match: no substitute dispatcher");
|
|
816
|
+
assert.equal(getGlobalDispatcher(), proxy, "the installed custom dispatcher stands");
|
|
817
|
+
|
|
818
|
+
const suffix = transport.dispatcherFor(900_000, "https://chat.lan.example/v1/chat/completions");
|
|
819
|
+
assert.equal(suffix, undefined, "leading-dot suffix match exempts the URL too");
|
|
820
|
+
|
|
821
|
+
const portMiss = transport.dispatcherFor(900_000, "https://model.internal/v1/chat/completions");
|
|
822
|
+
assert.ok(portMiss instanceof EnvHttpProxyAgent, "a non-matching port is not exempt");
|
|
823
|
+
await portMiss.close();
|
|
824
|
+
|
|
825
|
+
process.env.NO_PROXY = "other.example";
|
|
826
|
+
const covered = transport.dispatcherFor(900_000, "http://model.internal/v1/chat/completions");
|
|
827
|
+
assert.ok(covered instanceof EnvHttpProxyAgent, "a URL outside NO_PROXY still chains the proxy agent");
|
|
828
|
+
await covered.close();
|
|
829
|
+
} finally {
|
|
830
|
+
for (const [key, value] of Object.entries(prior)) {
|
|
831
|
+
if (value !== undefined) process.env[key] = value;
|
|
832
|
+
else delete process.env[key];
|
|
833
|
+
}
|
|
834
|
+
setGlobalDispatcher(original);
|
|
835
|
+
await proxy.close();
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
test("an empty lowercase proxy variable cannot hide a set uppercase one", async () => {
|
|
840
|
+
// undici's raw env read is `process.env.https_proxy ?? process.env.HTTPS_PROXY`
|
|
841
|
+
// — an empty lowercase string is not undefined, so it would shadow the URI.
|
|
842
|
+
// The resolved environment skips empty values; the gate and the
|
|
843
|
+
// EnvHttpProxyAgent overrides must agree on the uppercase URI.
|
|
844
|
+
const prior = {
|
|
845
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY,
|
|
846
|
+
https_proxy: process.env.https_proxy,
|
|
847
|
+
HTTP_PROXY: process.env.HTTP_PROXY,
|
|
848
|
+
http_proxy: process.env.http_proxy,
|
|
849
|
+
NO_PROXY: process.env.NO_PROXY,
|
|
850
|
+
no_proxy: process.env.no_proxy,
|
|
851
|
+
};
|
|
852
|
+
try {
|
|
853
|
+
for (const key of Object.keys(prior)) delete process.env[key];
|
|
854
|
+
process.env.HTTPS_PROXY = "http://upper.example:3128";
|
|
855
|
+
process.env.https_proxy = "";
|
|
856
|
+
assert.equal(resolveProxyEnv().httpsProxy, "http://upper.example:3128");
|
|
857
|
+
|
|
858
|
+
const original = getGlobalDispatcher();
|
|
859
|
+
const proxy = new ProxyAgent("http://127.0.0.1:9");
|
|
860
|
+
try {
|
|
861
|
+
setGlobalDispatcher(proxy);
|
|
862
|
+
const transport = new ChatTransport();
|
|
863
|
+
const chained = transport.dispatcherFor(900_000);
|
|
864
|
+
assert.ok(
|
|
865
|
+
chained instanceof EnvHttpProxyAgent,
|
|
866
|
+
"the empty lowercase value must not suppress the chained agent",
|
|
867
|
+
);
|
|
868
|
+
await chained.close();
|
|
869
|
+
} finally {
|
|
870
|
+
setGlobalDispatcher(original);
|
|
871
|
+
await proxy.close();
|
|
872
|
+
}
|
|
873
|
+
} finally {
|
|
874
|
+
for (const [key, value] of Object.entries(prior)) {
|
|
875
|
+
if (value !== undefined) process.env[key] = value;
|
|
876
|
+
else delete process.env[key];
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
test("the extraction route floors the dispatcher budget at fifteen minutes and leaves the abort budget to the operator", () => {
|
|
882
|
+
// The floor widens ONLY the transport's inactivity
|
|
883
|
+
// budgets (a proxyline host's 300s dispatcher cap must not dead-letter a
|
|
884
|
+
// longer configured budget); the operator's localLlmTimeoutMs still bounds
|
|
885
|
+
// the call through the abort signal (the drain engine's contract).
|
|
886
|
+
const floored = routeBudgets("extraction", 300, undefined);
|
|
887
|
+
assert.equal(floored.effectiveTimeoutMs, 300, "the abort budget stays the operator's");
|
|
888
|
+
assert.equal(floored.transportBudgetMs, EXTRACTION_MIN_TRANSPORT_BUDGET_MS, "the dispatcher budget carries the floor");
|
|
889
|
+
const explicit = routeBudgets("extraction", 600_000, 30_000);
|
|
890
|
+
assert.equal(explicit.effectiveTimeoutMs, 30_000, "an explicit operator timeout stays clamped by the ceiling");
|
|
891
|
+
assert.equal(explicit.transportBudgetMs, EXTRACTION_MIN_TRANSPORT_BUDGET_MS);
|
|
892
|
+
const other = routeBudgets("chat", 300, undefined);
|
|
893
|
+
assert.equal(other.effectiveTimeoutMs, 300);
|
|
894
|
+
assert.equal(other.transportBudgetMs, 300, "other routes keep the generic ceiling");
|
|
895
|
+
});
|
|
896
|
+
|
|
897
|
+
test("the extraction transport floor is fifteen minutes, well past undici's 300s default", () => {
|
|
898
|
+
// The issue-#2148 failure died at undici's 300s default and at the 300s-class
|
|
899
|
+
// generic budgets; the floor must clear 301s with room to spare.
|
|
900
|
+
assert.equal(EXTRACTION_MIN_TRANSPORT_BUDGET_MS, 900_000);
|
|
901
|
+
assert.ok(EXTRACTION_MIN_TRANSPORT_BUDGET_MS >= 301_000);
|
|
902
|
+
});
|
package/src/local-llm-helpers.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ResponseFormatJSONSchema } from "openai/resources/shared";
|
|
2
|
-
import type
|
|
2
|
+
import { fetch as undiciFetch, type Response as UndiciResponse } from "undici";
|
|
3
3
|
import { log } from "./logger.js";
|
|
4
|
-
import { directProbeDispatcher } from "./local-llm-transport.js";
|
|
4
|
+
import { directProbeDispatcher, isInvalidDispatcherArg, type TransportRequestInit } from "./local-llm-transport.js";
|
|
5
5
|
import type { LocalLlmRequestPriority } from "./local-llm.js";
|
|
6
6
|
|
|
7
7
|
export interface LocalLlmChatCompletionOptions {
|
|
@@ -181,10 +181,20 @@ export async function probeFetch(
|
|
|
181
181
|
: controller.signal;
|
|
182
182
|
const timeout = setTimeout(() => controller.abort(), options.timeoutMs);
|
|
183
183
|
try {
|
|
184
|
-
const init:
|
|
184
|
+
const init: TransportRequestInit = { signal: requestSignal, headers: options.headers };
|
|
185
185
|
const dispatcher = directProbeDispatcher();
|
|
186
186
|
if (dispatcher) init.dispatcher = dispatcher;
|
|
187
|
-
|
|
187
|
+
let response: Response | UndiciResponse;
|
|
188
|
+
try {
|
|
189
|
+
response = await fetch(url, init);
|
|
190
|
+
} catch (err) {
|
|
191
|
+
// node >= 26 wraps dispatch failures in TypeError("fetch failed"),
|
|
192
|
+
// so UND_ERR_INVALID_ARG can ride `err` or its cause chain; the
|
|
193
|
+
// bundled undici's fetch accepts the same init on every supported
|
|
194
|
+
// Node, so direct-mode probes still run there.
|
|
195
|
+
if (!(err instanceof Error) || !isInvalidDispatcherArg(err)) throw err;
|
|
196
|
+
response = await undiciFetch(url, init);
|
|
197
|
+
}
|
|
188
198
|
clearTimeout(timeout);
|
|
189
199
|
if (!response.ok) {
|
|
190
200
|
log.debug(`local LLM probe: ${url} returned HTTP ${response.status}`);
|
|
@@ -1,7 +1,167 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Agent,
|
|
3
|
+
EnvHttpProxyAgent,
|
|
4
|
+
MockAgent,
|
|
5
|
+
fetch as undiciFetch,
|
|
6
|
+
getGlobalDispatcher,
|
|
7
|
+
type Dispatcher,
|
|
8
|
+
type Response as UndiciResponse,
|
|
9
|
+
} from "undici";
|
|
2
10
|
|
|
3
11
|
import { log } from "./logger.js";
|
|
4
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Transport floor for the extraction route (issue #2148 root cause):
|
|
15
|
+
* extraction completions routinely run past undici's default 300s header
|
|
16
|
+
* budget, and the generic `localLlmTimeoutMs` ceiling is sized for chat. The
|
|
17
|
+
* extraction operation's dispatcher inactivity budgets are floored at this
|
|
18
|
+
* value; the call's abort budget still follows the ceiling/operator bound and
|
|
19
|
+
* the caller's own cancellation signal still ends the request early.
|
|
20
|
+
*/
|
|
21
|
+
export const EXTRACTION_MIN_TRANSPORT_BUDGET_MS = 900_000;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Route-sized budgets: the generic ceiling is `min(ceiling, operator)`,
|
|
25
|
+
* except the extraction route, whose dispatcher inactivity budgets are
|
|
26
|
+
* floored at EXTRACTION_MIN_TRANSPORT_BUDGET_MS (extraction completions
|
|
27
|
+
* routinely outlast chat-sized budgets; issue #2148 root cause). The abort
|
|
28
|
+
* budget (effectiveTimeoutMs) is not floored.
|
|
29
|
+
*/
|
|
30
|
+
export interface RouteBudgets {
|
|
31
|
+
effectiveTimeoutMs: number;
|
|
32
|
+
transportBudgetMs: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function routeBudgets(
|
|
36
|
+
operation: string,
|
|
37
|
+
ceilingMs: number,
|
|
38
|
+
operatorMs?: number,
|
|
39
|
+
): RouteBudgets {
|
|
40
|
+
const routeFloorMs =
|
|
41
|
+
operation === "extraction" ? EXTRACTION_MIN_TRANSPORT_BUDGET_MS : 0;
|
|
42
|
+
const operatorBudgetMs = operatorMs ?? ceilingMs;
|
|
43
|
+
// The operator's budget (the signal's ceiling) bounds the call; the floor
|
|
44
|
+
// widens only the transport's inactivity budgets, so a proxyline host's
|
|
45
|
+
// 300s dispatcher cap cannot dead-letter a longer configured budget.
|
|
46
|
+
const effectiveTimeoutMs = Math.min(ceilingMs, operatorBudgetMs);
|
|
47
|
+
const transportBudgetMs = routeFloorMs
|
|
48
|
+
? Math.max(ceilingMs, routeFloorMs)
|
|
49
|
+
: ceilingMs;
|
|
50
|
+
return { effectiveTimeoutMs, transportBudgetMs };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The standard egress-proxy environment variables, undici's lowercase-first
|
|
54
|
+
* read order (the `lower ?? upper` read inside EnvHttpProxyAgent). */
|
|
55
|
+
const HTTP_PROXY_KEYS = ["http_proxy", "HTTP_PROXY"] as const;
|
|
56
|
+
const HTTPS_PROXY_KEYS = ["https_proxy", "HTTPS_PROXY"] as const;
|
|
57
|
+
const NO_PROXY_KEYS = ["no_proxy", "NO_PROXY"] as const;
|
|
58
|
+
const PROXY_ENV_KEYS = [...HTTP_PROXY_KEYS, ...HTTPS_PROXY_KEYS] as const;
|
|
59
|
+
|
|
60
|
+
function envValue(keys: readonly string[]): string | undefined {
|
|
61
|
+
// An empty variable must not hide a later one: HTTPS_PROXY="" means "unset",
|
|
62
|
+
// not "the proxy is the empty string".
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
const value = process.env[key]?.trim();
|
|
65
|
+
if (value) return value;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The egress-proxy environment after resolution: lowercase-first read order
|
|
72
|
+
* with empty values treated as unset. The same resolved values are handed
|
|
73
|
+
* to EnvHttpProxyAgent's overrides, so the gate and the pool can never
|
|
74
|
+
* disagree about which proxy applies (undici's own read skips only unset
|
|
75
|
+
* variables — an empty lowercase value would hide a set uppercase one).
|
|
76
|
+
*/
|
|
77
|
+
export interface ResolvedProxyEnv {
|
|
78
|
+
httpProxy?: string;
|
|
79
|
+
httpsProxy?: string;
|
|
80
|
+
noProxy?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function resolveProxyEnv(): ResolvedProxyEnv {
|
|
84
|
+
return {
|
|
85
|
+
httpProxy: envValue(HTTP_PROXY_KEYS),
|
|
86
|
+
httpsProxy: envValue(HTTPS_PROXY_KEYS),
|
|
87
|
+
noProxy: envValue(NO_PROXY_KEYS),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function proxyUriFromEnv(): string | undefined {
|
|
92
|
+
const { httpsProxy, httpProxy } = resolveProxyEnv();
|
|
93
|
+
return httpsProxy ?? httpProxy;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** undici's DEFAULT_PORTS for the schemes the local endpoint uses. */
|
|
97
|
+
function defaultPortFor(protocol: string): number {
|
|
98
|
+
return protocol === "http:" ? 80 : protocol === "https:" ? 443 : 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* EnvHttpProxyAgent's `#shouldProxy` semantics, mirrored: true when `url`'s
|
|
103
|
+
* host and port are exempt from the proxy environment via NO_PROXY/no_proxy.
|
|
104
|
+
*/
|
|
105
|
+
function noProxyBypasses(url: string, noProxy: string | undefined): boolean {
|
|
106
|
+
if (!noProxy) return false;
|
|
107
|
+
if (noProxy === "*") return true;
|
|
108
|
+
let hostname: string;
|
|
109
|
+
let port: number;
|
|
110
|
+
try {
|
|
111
|
+
const parsed = new URL(url);
|
|
112
|
+
hostname = parsed.host.replace(/:\d*$/, "").toLowerCase();
|
|
113
|
+
port = Number.parseInt(parsed.port, 10) || defaultPortFor(parsed.protocol);
|
|
114
|
+
} catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
for (const entry of noProxy.split(/[,\s]/)) {
|
|
118
|
+
if (!entry) continue;
|
|
119
|
+
const hostPort = entry.match(/^(.+):(\d+)$/);
|
|
120
|
+
const entryHost = (hostPort ? hostPort[1] : entry).toLowerCase();
|
|
121
|
+
const entryPort = hostPort ? Number.parseInt(hostPort[2], 10) : 0;
|
|
122
|
+
if (entryPort && entryPort !== port) continue;
|
|
123
|
+
if (!/^[.*]/.test(entryHost)) {
|
|
124
|
+
if (hostname === entryHost) return true;
|
|
125
|
+
} else if (hostname.endsWith(entryHost.replace(/^\*/, ""))) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The per-request init shape the transport hands fetch: undici's RequestInit
|
|
134
|
+
* (minus the body union this client never uses — chat bodies are strings)
|
|
135
|
+
* plus the dispatcher the #2148 fix passes per request.
|
|
136
|
+
*/
|
|
137
|
+
export type TransportRequestInit = Omit<RequestInit, "body" | "dispatcher"> & {
|
|
138
|
+
body?: string;
|
|
139
|
+
dispatcher?: Dispatcher;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* True when `err` or its cause chain (node 26 wraps dispatch failures in
|
|
144
|
+
* TypeError("fetch failed")) carries undici's UND_ERR_INVALID_ARG — the
|
|
145
|
+
* runtime's rejection of a per-request dispatcher.
|
|
146
|
+
*/
|
|
147
|
+
export function isInvalidDispatcherArg(err: Error): boolean {
|
|
148
|
+
let current: unknown = err;
|
|
149
|
+
for (let depth = 0; current instanceof Error && depth < 5; depth += 1) {
|
|
150
|
+
// SAFETY: undici codes ride NodeJS.ErrnoException-style `code` and `cause` members.
|
|
151
|
+
const errno = current as NodeJS.ErrnoException;
|
|
152
|
+
if (errno.code === "UND_ERR_INVALID_ARG") return true;
|
|
153
|
+
current = errno.cause;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Either wire response: the runtime's (global fetch) or undici's (the
|
|
160
|
+
* dispatcher-carrying fallback for runtimes that reject per-request
|
|
161
|
+
* dispatchers). Every consumer only reads ok/status/headers/text/json.
|
|
162
|
+
*/
|
|
163
|
+
export type ChatWireResponse = Response | UndiciResponse;
|
|
164
|
+
|
|
5
165
|
/**
|
|
6
166
|
* Wire transport for local LLM chat completions.
|
|
7
167
|
*
|
|
@@ -70,32 +230,100 @@ export class ChatTransport {
|
|
|
70
230
|
private agent: Agent | null = null;
|
|
71
231
|
private agentBudgetMs: number | null = null;
|
|
72
232
|
private warnedAboutCustomDispatcher = false;
|
|
233
|
+
/** Chained proxy pool: the proxyline stays in the path with widened budgets. */
|
|
234
|
+
private proxyAgent: EnvHttpProxyAgent | null = null;
|
|
235
|
+
private proxyKey: string | null = null;
|
|
73
236
|
|
|
74
237
|
/**
|
|
75
238
|
* The widened pool, or `undefined` when the process installed its own
|
|
76
239
|
* transport that we must not displace.
|
|
77
240
|
*
|
|
78
241
|
* A deployment can route the local endpoint through a process-wide
|
|
79
|
-
* dispatcher —
|
|
80
|
-
* connect/TLS/DNS
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
242
|
+
* dispatcher — an agent runtime's managed proxy ("proxyline") or a custom
|
|
243
|
+
* connect/TLS/DNS transport.
|
|
244
|
+
*
|
|
245
|
+
* - When the standard egress-proxy environment variables name the proxy,
|
|
246
|
+
* the transport chains its own `ProxyAgent` over that URI with the
|
|
247
|
+
* widened budgets: the proxyline stays in the path and the budgets take
|
|
248
|
+
* effect. This is the issue-#2148 root cause fix — the managed proxyline
|
|
249
|
+
* dispatcher kept undici's 300s `headersTimeout`, so extraction
|
|
250
|
+
* completions slower than five minutes died.
|
|
251
|
+
* - A NO_PROXY/no_proxy exemption on the request host routes it direct,
|
|
252
|
+
* but a plain direct pool cannot reproduce a custom dispatcher's
|
|
253
|
+
* DNS/connect/TLS logic — the installed transport stays in place.
|
|
254
|
+
* - With no proxy environment to mirror, the installed dispatcher is left
|
|
255
|
+
* alone and the 300s cap stands — only its owner knows how to rebuild
|
|
256
|
+
* it. The trade-off is logged once.
|
|
257
|
+
* - `REMNIC_LOCAL_LLM_DIRECT=1` declares the local endpoint is reached
|
|
258
|
+
* directly even though a global dispatcher exists; the widened direct
|
|
259
|
+
* pool is used, bypassing the proxy.
|
|
260
|
+
*
|
|
261
|
+
* A `MockAgent` is excluded by the same check, so test transports keep
|
|
262
|
+
* working.
|
|
85
263
|
*/
|
|
86
|
-
dispatcherFor(budgetMs: number): Agent | undefined {
|
|
87
|
-
|
|
88
|
-
|
|
264
|
+
dispatcherFor(budgetMs: number, url?: string): Agent | EnvHttpProxyAgent | undefined {
|
|
265
|
+
const normalized = Number.isFinite(budgetMs) ? Math.max(0, budgetMs) : 0;
|
|
266
|
+
const installed = getGlobalDispatcher();
|
|
267
|
+
const custom = !isPlainPoolingAgent(installed);
|
|
268
|
+
// A MockAgent is a test transport: it is left in place like any other
|
|
269
|
+
// dispatcher the transport cannot mirror. The constructor-name check
|
|
270
|
+
// covers a MockAgent from a different undici copy than the one we import
|
|
271
|
+
// (same reason isPlainPoolingAgent checks ctor.name).
|
|
272
|
+
const installedIsMock =
|
|
273
|
+
custom && (installed instanceof MockAgent || installed.constructor.name === "MockAgent");
|
|
274
|
+
if (custom && process.env.REMNIC_LOCAL_LLM_DIRECT !== "1") {
|
|
275
|
+
const proxyEnv = installedIsMock ? undefined : resolveProxyEnv();
|
|
276
|
+
const proxyUri = proxyEnv ? proxyEnv.httpsProxy ?? proxyEnv.httpProxy : undefined;
|
|
277
|
+
if (proxyUri) {
|
|
278
|
+
if (url && noProxyBypasses(url, proxyEnv?.noProxy)) {
|
|
279
|
+
// The environment routes this host direct — but a fresh direct pool
|
|
280
|
+
// cannot reproduce the installed transport's custom DNS/connect
|
|
281
|
+
// logic, so it stays. The 300s trade-off is logged once.
|
|
282
|
+
if (!this.warnedAboutCustomDispatcher) {
|
|
283
|
+
this.warnedAboutCustomDispatcher = true;
|
|
284
|
+
log.debug(
|
|
285
|
+
"local LLM: NO_PROXY exempts the local endpoint from the proxy environment; " +
|
|
286
|
+
"leaving the installed custom dispatcher in place. " +
|
|
287
|
+
"localLlmTimeoutMs above that dispatcher's headersTimeout will not take effect.",
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
// The chained agent's behavior tracks the whole proxy environment, so
|
|
293
|
+
// the cache key must too: a changed HTTP_PROXY or NO_PROXY with a
|
|
294
|
+
// steady HTTPS_PROXY must not hand back the stale pool.
|
|
295
|
+
const key = [
|
|
296
|
+
proxyUri,
|
|
297
|
+
...PROXY_ENV_KEYS.map((k) => process.env[k] ?? ""),
|
|
298
|
+
...NO_PROXY_KEYS.map((k) => process.env[k] ?? ""),
|
|
299
|
+
normalized,
|
|
300
|
+
].join("|");
|
|
301
|
+
if (this.proxyAgent && this.proxyKey === key) return this.proxyAgent;
|
|
302
|
+
void this.proxyAgent?.close().catch(() => {
|
|
303
|
+
/* pool teardown is best-effort */
|
|
304
|
+
});
|
|
305
|
+
// EnvHttpProxyAgent keeps NO_PROXY and per-protocol proxy selection;
|
|
306
|
+
// the resolved values are passed as overrides so its raw env read can
|
|
307
|
+
// never disagree with the gate's resolution (empty-lowercase skew).
|
|
308
|
+
this.proxyAgent = new EnvHttpProxyAgent({
|
|
309
|
+
httpProxy: proxyEnv?.httpProxy,
|
|
310
|
+
httpsProxy: proxyEnv?.httpsProxy,
|
|
311
|
+
noProxy: proxyEnv?.noProxy,
|
|
312
|
+
headersTimeout: normalized,
|
|
313
|
+
bodyTimeout: normalized,
|
|
314
|
+
});
|
|
315
|
+
this.proxyKey = key;
|
|
316
|
+
return this.proxyAgent;
|
|
317
|
+
}
|
|
89
318
|
if (!this.warnedAboutCustomDispatcher) {
|
|
90
319
|
this.warnedAboutCustomDispatcher = true;
|
|
91
320
|
log.debug(
|
|
92
|
-
"local LLM: a custom global dispatcher is installed; leaving it in place. " +
|
|
321
|
+
"local LLM: a custom global dispatcher is installed and no proxy environment names it; leaving it in place. " +
|
|
93
322
|
"localLlmTimeoutMs above that dispatcher's headersTimeout will not take effect.",
|
|
94
323
|
);
|
|
95
324
|
}
|
|
96
325
|
return undefined;
|
|
97
326
|
}
|
|
98
|
-
const normalized = Number.isFinite(budgetMs) ? Math.max(0, budgetMs) : 0;
|
|
99
327
|
if (this.agent && this.agentBudgetMs === normalized) return this.agent;
|
|
100
328
|
void this.agent?.close().catch(() => {
|
|
101
329
|
/* pool teardown is best-effort */
|
|
@@ -108,15 +336,24 @@ export class ChatTransport {
|
|
|
108
336
|
return this.agent;
|
|
109
337
|
}
|
|
110
338
|
|
|
111
|
-
async post(request: ChatCompletionRequest): Promise<Response> {
|
|
112
|
-
const init:
|
|
339
|
+
async post(request: ChatCompletionRequest): Promise<Response | UndiciResponse> {
|
|
340
|
+
const init: TransportRequestInit = {
|
|
113
341
|
method: "POST",
|
|
114
342
|
headers: request.headers,
|
|
115
343
|
body: request.body,
|
|
116
344
|
signal: request.signal,
|
|
117
345
|
};
|
|
118
|
-
const dispatcher = this.dispatcherFor(request.budgetMs);
|
|
346
|
+
const dispatcher = this.dispatcherFor(request.budgetMs, request.url);
|
|
119
347
|
if (dispatcher) init.dispatcher = dispatcher;
|
|
120
|
-
|
|
348
|
+
try {
|
|
349
|
+
return await fetch(request.url, init);
|
|
350
|
+
} catch (err) {
|
|
351
|
+
// node >= 26 wraps dispatch failures in TypeError("fetch failed"),
|
|
352
|
+
// so UND_ERR_INVALID_ARG can ride `err` or its cause chain; the
|
|
353
|
+
// bundled undici's fetch accepts the same init on every supported
|
|
354
|
+
// Node, so the widened budget still rides.
|
|
355
|
+
if (!(err instanceof Error) || !isInvalidDispatcherArg(err)) throw err;
|
|
356
|
+
return await undiciFetch(request.url, init);
|
|
357
|
+
}
|
|
121
358
|
}
|
|
122
359
|
}
|