dsh-remote-vs 0.29.0 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +14 -3
- package/README.md +10 -3
- package/lib/client.js +264 -88
- package/lib/index.js +821 -127
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/changes/recorder.d.ts +65 -10
- package/lib/types/changes/status.d.ts +48 -3
- package/lib/types/client/ConnectionsSection.d.ts +73 -0
- package/lib/types/client/api.d.ts +41 -2
- package/lib/types/client/scm/actions.d.ts +25 -1
- package/lib/types/p2/helper-rpc.d.ts +48 -0
- package/lib/types/p2/worlds.d.ts +28 -0
- package/lib/types/pool.d.ts +245 -10
- package/lib/types/protocol.d.ts +96 -0
- package/lib/{worlds-RG8K5eom.js → worlds-CRRZMZhg.js} +85 -6
- package/package.json +1 -1
- package/src/changes/recorder.ts +151 -15
- package/src/changes/status.ts +59 -4
- package/src/client/ConnectionsSection.tsx +359 -0
- package/src/client/MonitorView.tsx +14 -0
- package/src/client/api.ts +72 -4
- package/src/client/i18n.ts +50 -2
- package/src/client/scm/actions.ts +63 -12
- package/src/client/vsc/vsc.css +176 -0
- package/src/p2/helper-rpc.ts +73 -5
- package/src/p2/worlds.ts +48 -0
- package/src/pool.ts +736 -111
- package/src/protocol.ts +100 -0
- package/src/routes.ts +66 -0
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as dshHome, D as viewSettingsPath, E as storePath, S as aliasRoot, T as keysDir, _ as MAX_TIMEOUT_MS, a as takeHostNotices, b as resolveTimeouts, c as pushPipeline, d as WriteConflictError, f as attachmentHeader, g as DeadlineError, h as DEFAULT_TIMEOUTS, i as drainHostNotices, l as seedHelperCache, m as DEFAULT_READ_TIMEOUT_MS, n as TombstonedError, o as helperAvailability, p as DEFAULT_GIT_TIMEOUT_MS, s as probe, t as RemoteWorlds, u as RemoteFiles, v as MIN_TIMEOUT_MS, w as ensurePrivateDir, x as require_lib, y as clampExecTimeout } from "./worlds-
|
|
1
|
+
import { C as dshHome, D as viewSettingsPath, E as storePath, S as aliasRoot, T as keysDir, _ as MAX_TIMEOUT_MS, a as takeHostNotices, b as resolveTimeouts, c as pushPipeline, d as WriteConflictError, f as attachmentHeader, g as DeadlineError, h as DEFAULT_TIMEOUTS, i as drainHostNotices, l as seedHelperCache, m as DEFAULT_READ_TIMEOUT_MS, n as TombstonedError, o as helperAvailability, p as DEFAULT_GIT_TIMEOUT_MS, s as probe, t as RemoteWorlds, u as RemoteFiles, v as MIN_TIMEOUT_MS, w as ensurePrivateDir, x as require_lib, y as clampExecTimeout } from "./worlds-CRRZMZhg.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
3
|
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, statSync, writeSync } from "node:fs";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
@@ -1253,6 +1253,8 @@ const REMOTE_API = {
|
|
|
1253
1253
|
viewSettings: "/api/dsh-remote/view-settings",
|
|
1254
1254
|
/** Profile-wide proxy default (per-host overrides live on the host rows). */
|
|
1255
1255
|
proxySettings: "/api/dsh-remote/proxy-settings",
|
|
1256
|
+
/** Live SSH pool shape per host: connections, channels, slot occupancy. */
|
|
1257
|
+
connections: "/api/dsh-remote/connections",
|
|
1256
1258
|
/** Current working directory of one live session (click resolution helper). */
|
|
1257
1259
|
sessionCwd: "/api/dsh-remote/session-cwd",
|
|
1258
1260
|
/** What one live session changed on its remote host (the review panel). */
|
|
@@ -1463,6 +1465,31 @@ const FORWARD_DEDICATED_BUDGET_MS = (() => {
|
|
|
1463
1465
|
*/
|
|
1464
1466
|
const FORWARD_DEDICATED_BLOCK_MS = 6e4;
|
|
1465
1467
|
/**
|
|
1468
|
+
* How long a file operation waits for its DEDICATED SFTP connection before
|
|
1469
|
+
* falling back to the shared one.
|
|
1470
|
+
*
|
|
1471
|
+
* Same argument as {@link FORWARD_DEDICATED_BUDGET_MS}, and it binds harder
|
|
1472
|
+
* here: browsing fires one request per folder open (plus a stat per entry), so a
|
|
1473
|
+
* server that silently DROPS the extra connection would otherwise stall the
|
|
1474
|
+
* explorer for the whole connect watchdog on EVERY click. The dedicated
|
|
1475
|
+
* connection is an optimization; it must never be what the user waits on.
|
|
1476
|
+
*
|
|
1477
|
+
* Override with DSH_REMOTE_VS_SFTP_BUDGET_MS (milliseconds, >= 10).
|
|
1478
|
+
*/
|
|
1479
|
+
const SFTP_DEDICATED_BUDGET_MS = (() => {
|
|
1480
|
+
const raw = Number(process.env.DSH_REMOTE_VS_SFTP_BUDGET_MS);
|
|
1481
|
+
return Number.isFinite(raw) && raw >= 10 ? raw : 3e3;
|
|
1482
|
+
})();
|
|
1483
|
+
/**
|
|
1484
|
+
* How long a refused dedicated SFTP connection is skipped per alias.
|
|
1485
|
+
*
|
|
1486
|
+
* Without it the fallback would be re-dialed by every browse request — the exact
|
|
1487
|
+
* waiting the dedicated budget above just removed. One minute covers a burst of
|
|
1488
|
+
* browsing; `drop(alias)` (host edited/deleted) and a successful dedicated
|
|
1489
|
+
* connection clear it immediately.
|
|
1490
|
+
*/
|
|
1491
|
+
const SFTP_DEDICATED_BLOCK_MS = 6e4;
|
|
1492
|
+
/**
|
|
1466
1493
|
* Connection establishment timeout, including authentication.
|
|
1467
1494
|
*
|
|
1468
1495
|
* This was 15s, which is not a handshake budget so much as a CPU budget: on a
|
|
@@ -1531,20 +1558,116 @@ const CONTROL_TIMEOUT_MS = 15e3;
|
|
|
1531
1558
|
*/
|
|
1532
1559
|
const GROUP_KILL_GRACE_MS = 3e3;
|
|
1533
1560
|
/**
|
|
1534
|
-
* Concurrent channels the pool opens per alias
|
|
1535
|
-
*
|
|
1536
|
-
*
|
|
1537
|
-
*
|
|
1538
|
-
*
|
|
1539
|
-
*
|
|
1561
|
+
* Concurrent EXEC channels the pool opens per alias on the SHARED connection.
|
|
1562
|
+
*
|
|
1563
|
+
* The server's own `MaxSessions` (10 on the hosts this was measured against) is
|
|
1564
|
+
* the real ceiling, and a channel past it is refused HARD — ssh2 reports
|
|
1565
|
+
* "administratively prohibited" and nobody retries, so the operation fails in a
|
|
1566
|
+
* way that looks like a broken host. Queueing is the readable alternative, which
|
|
1567
|
+
* is why every counted channel kind goes through a budget instead.
|
|
1568
|
+
*
|
|
1569
|
+
* The counting is now honest — every `conn.exec` channel takes a slot:
|
|
1570
|
+
* - `runExec` (every ordinary command) and `execChannel` (the subprocess
|
|
1571
|
+
* runtime's long-lived channels, see `p2/ssh-proc.ts`), both on this budget;
|
|
1572
|
+
* - network verbs ({@link NET_CHANNEL_LIMIT}) and git commands
|
|
1573
|
+
* ({@link GIT_CHANNEL_LIMIT}), on their own budgets on the same connection.
|
|
1574
|
+
* PTY terminals, port forwards and SFTP sessions ride their OWN connections, so
|
|
1575
|
+
* they cannot exhaust this allowance at all.
|
|
1576
|
+
*
|
|
1577
|
+
* **The arithmetic (shared connection, measured `MaxSessions` 10)**:
|
|
1578
|
+
* counted channels are exec 4 + network 2 + git 3 = **9**. The tenth session is
|
|
1579
|
+
* deliberately left free for the only channels that are still NOT counted: the
|
|
1580
|
+
* pool's own housekeeping (`bare` — the one-shot `setsid` capability probe and
|
|
1581
|
+
* the group kill). Those are transient, but they must never be refused, because
|
|
1582
|
+
* a refused probe silently degrades the timeout path and a refused kill leaks a
|
|
1583
|
+
* remote process. So 9 + 1 is a BOUNDED DESIGN, not spare headroom: a tenth
|
|
1584
|
+
* counted channel would put the sum at 10 only as long as nothing transient ever
|
|
1585
|
+
* fires — an assumption the pool cannot make.
|
|
1586
|
+
*
|
|
1587
|
+
* **Why 4 and not more** — this is a deliberate split of the same nine, not a
|
|
1588
|
+
* reduction of it. The field call: "several workspaces / repositories scanning at
|
|
1589
|
+
* once" is the common shape, while "an agent running many exec commands at once"
|
|
1590
|
+
* is rarer, so one slot moved from exec to the git lane (git 2 → 3) and the
|
|
1591
|
+
* network lane stayed at two (push and pull must not serialize). The known cost:
|
|
1592
|
+
* exec concurrency drops from five to four, so an agent's background commands plus
|
|
1593
|
+
* a monitor sample plus the process panel plus the listening-port enumeration can
|
|
1594
|
+
* fill it and the fifth waits — bounded at 30 s with a readable message, and
|
|
1595
|
+
* visible as `Shared 4/4 · N queued` in the monitor. If exec turns out to be the
|
|
1596
|
+
* real bottleneck, move the slot back; the total must stay 9.
|
|
1597
|
+
*
|
|
1598
|
+
* The budget is PER CONNECTION, and the pool holds up to four connections per
|
|
1599
|
+
* alias, so the other classes (PTY/forward/SFTP) keep their own ceilings.
|
|
1540
1600
|
*/
|
|
1541
|
-
const CHANNEL_LIMIT =
|
|
1601
|
+
const CHANNEL_LIMIT = 4;
|
|
1602
|
+
/**
|
|
1603
|
+
* Concurrent SFTP sessions on the DEDICATED file connection.
|
|
1604
|
+
*
|
|
1605
|
+
* Deliberately its own number rather than {@link CHANNEL_LIMIT}: the shared
|
|
1606
|
+
* connection's ceiling is tight because three budgets share it with the transient
|
|
1607
|
+
* `bare` probe/kill, while the file connection carries nothing but SFTP sessions
|
|
1608
|
+
* and has the whole `MaxSessions` (10) to itself. Six is what the transfer
|
|
1609
|
+
* measurement landed on (the extra channel windows a download may open), and it is
|
|
1610
|
+
* unchanged by the exec/network/git rebalance.
|
|
1611
|
+
*/
|
|
1612
|
+
const SFTP_CHANNEL_LIMIT = 6;
|
|
1613
|
+
/**
|
|
1614
|
+
* Concurrent NETWORK verbs (`git fetch` / `pull` / `push` / `sync`) per alias.
|
|
1615
|
+
*
|
|
1616
|
+
* They ride the SHARED connection (no fifth link: the traffic is not the problem,
|
|
1617
|
+
* the HOLD TIME is) but get their own budget, because their timeout is measured
|
|
1618
|
+
* in minutes — 20 by default. On the exec budget one stuck push pinned one of the
|
|
1619
|
+
* exec slots for the whole budget, and a couple of them filled the table: status
|
|
1620
|
+
* refreshes, the process panel, the monitor and every agent tool then queued 30 s
|
|
1621
|
+
* and failed with "host is busy". With a separate table that can no longer happen.
|
|
1622
|
+
*
|
|
1623
|
+
* Two is enough for the real concurrency (a push and a pull side by side; `sync`
|
|
1624
|
+
* runs its steps sequentially). A third verb waits, and says so in its own words
|
|
1625
|
+
* (see `budgetOf`). The full per-connection arithmetic lives on
|
|
1626
|
+
* {@link CHANNEL_LIMIT}: exec 4 + network 2 + git 3 = 9 counted, with one session
|
|
1627
|
+
* left free for the transient `bare` probe/kill.
|
|
1628
|
+
*/
|
|
1629
|
+
const NET_CHANNEL_LIMIT = 2;
|
|
1630
|
+
/**
|
|
1631
|
+
* Concurrent GIT commands (`git status` / `diff` / `log` reads AND the local
|
|
1632
|
+
* writes — commit, stage, discard, branch create/rename/delete, merge) per alias.
|
|
1633
|
+
*
|
|
1634
|
+
* Same argument as the network lane, from the other end of the timeout scale: a
|
|
1635
|
+
* big repository's `git status` alone is allowed 300 s by default
|
|
1636
|
+
* (`DEFAULT_GIT_TIMEOUT_MS`, up to 30 minutes), and every git command used to
|
|
1637
|
+
* hold one of the exec slots for its whole run — while the monitor, the process
|
|
1638
|
+
* panel, the ledger's own next scan and every agent tool queued behind it and
|
|
1639
|
+
* answered "host is busy" after 30 s.
|
|
1640
|
+
*
|
|
1641
|
+
* NOTE the lane is "git commands", not "repository reads": the SCM panel sends its
|
|
1642
|
+
* local writes (`git commit`, `git add`, `git restore`, branch verbs) here too,
|
|
1643
|
+
* because they are git commands with a git timeout. They are fast, and they no
|
|
1644
|
+
* longer occupy a slot out of the same few the monitor and the agent tools use.
|
|
1645
|
+
*
|
|
1646
|
+
* **Why three** (shared connection, measured `MaxSessions` 10):
|
|
1647
|
+
* - counted channels: exec 4 + network 2 + git 3 = 9, plus the one session kept
|
|
1648
|
+
* free for the transient `bare` probe/kill — see {@link CHANNEL_LIMIT};
|
|
1649
|
+
* - three covers the load the field actually shows: several open workspaces or
|
|
1650
|
+
* repositories scanning at once (each runs a tracked status AND, outside the
|
|
1651
|
+
* client queue, an untracked walk), which is more common than an agent running
|
|
1652
|
+
* many commands at the same instant — hence the extra slot came out of exec;
|
|
1653
|
+
* - four would make 10 counted with the probe reserve gone; the surplus channel
|
|
1654
|
+
* past `MaxSessions` is refused HARD by sshd ("administratively prohibited", no
|
|
1655
|
+
* retry) — the exact failure these budgets exist to replace with a bounded
|
|
1656
|
+
* queue. Raising this further means taking the slot from exec again.
|
|
1657
|
+
*/
|
|
1658
|
+
const GIT_CHANNEL_LIMIT = 3;
|
|
1542
1659
|
/**
|
|
1543
1660
|
* How long an operation waits for a channel slot before failing. Bounded on
|
|
1544
1661
|
* purpose: queueing forever behind a peer that never finishes is worse than a
|
|
1545
1662
|
* readable error that tells the user to retry.
|
|
1663
|
+
*
|
|
1664
|
+
* Override with DSH_REMOTE_VS_CHANNEL_WAIT_MS (milliseconds, >= 10) so a gate can
|
|
1665
|
+
* assert the "busy" message without waiting the production 30 s.
|
|
1546
1666
|
*/
|
|
1547
|
-
const CHANNEL_WAIT_MS =
|
|
1667
|
+
const CHANNEL_WAIT_MS = (() => {
|
|
1668
|
+
const raw = Number(process.env.DSH_REMOTE_VS_CHANNEL_WAIT_MS);
|
|
1669
|
+
return Number.isFinite(raw) && raw >= 10 ? raw : 3e4;
|
|
1670
|
+
})();
|
|
1548
1671
|
/**
|
|
1549
1672
|
* Idle time after which the shared BROWSE SFTP session is closed.
|
|
1550
1673
|
*
|
|
@@ -1612,6 +1735,38 @@ var RemotePool = class {
|
|
|
1612
1735
|
* stops working because of it — it only loses the isolation.
|
|
1613
1736
|
*/
|
|
1614
1737
|
forwardConns = /* @__PURE__ */ new Map();
|
|
1738
|
+
/**
|
|
1739
|
+
* Dedicated connections for FILE operations (one per alias).
|
|
1740
|
+
*
|
|
1741
|
+
* The third instance of the same argument. Browse/read/write/upload/download
|
|
1742
|
+
* used to ride the shared connection, which meant a `git status` burst or a
|
|
1743
|
+
* long exec could sit in front of a directory refresh (SSH offers no
|
|
1744
|
+
* per-channel priority), AND that the shared link's channel budget was shared
|
|
1745
|
+
* with file I/O: a few in-flight git/search commands and the explorer could not
|
|
1746
|
+
* open a channel at all. A file operation is exactly the traffic a user is
|
|
1747
|
+
* watching (a folder must open, a save must land), so it gets its own
|
|
1748
|
+
* connection and its own channel budget (`sftpSlots`).
|
|
1749
|
+
*
|
|
1750
|
+
* Cost: one extra handshake per alias that touches files, plus one held TCP
|
|
1751
|
+
* connection while any of its channels is open (the 30-minute idle eviction
|
|
1752
|
+
* applies once the last one ends). `acquireSftp` falls back to the
|
|
1753
|
+
* shared connection when this one cannot be established — a host that refuses
|
|
1754
|
+
* extra connections still serves files, it only loses the isolation.
|
|
1755
|
+
*/
|
|
1756
|
+
sftpConns = /* @__PURE__ */ new Map();
|
|
1757
|
+
/** Aliases whose dedicated-SFTP downgrade was already logged (once per alias). */
|
|
1758
|
+
sftpFallbackNoted = /* @__PURE__ */ new Set();
|
|
1759
|
+
/** alias -> timestamp until which the dedicated SFTP connection is skipped. */
|
|
1760
|
+
sftpBlocked = /* @__PURE__ */ new Map();
|
|
1761
|
+
/**
|
|
1762
|
+
* Aliases whose file operations are DOWNGRADED right now (sticky until a
|
|
1763
|
+
* dedicated connection succeeds again or the host is edited).
|
|
1764
|
+
*
|
|
1765
|
+
* Purely diagnostic: `stats()` reports it so the monitor page can say "these
|
|
1766
|
+
* files are riding the shared connection" instead of leaving the user to
|
|
1767
|
+
* wonder why a browse refresh still queues behind git.
|
|
1768
|
+
*/
|
|
1769
|
+
sftpFallback = /* @__PURE__ */ new Set();
|
|
1615
1770
|
/** Aliases whose dedicated-forward downgrade was already logged (once per alias). */
|
|
1616
1771
|
forwardFallbackNoted = /* @__PURE__ */ new Set();
|
|
1617
1772
|
/** alias -> timestamp until which the dedicated forward connection is skipped. */
|
|
@@ -1620,8 +1775,35 @@ var RemotePool = class {
|
|
|
1620
1775
|
hostKeys = /* @__PURE__ */ new Map();
|
|
1621
1776
|
/** Per-alias `setsid --wait` capability, probed once (see supportsSetsid). */
|
|
1622
1777
|
setsidProbes = /* @__PURE__ */ new Map();
|
|
1623
|
-
/**
|
|
1778
|
+
/**
|
|
1779
|
+
* Channel slots of the SHARED connection (see acquireSlot), and of the
|
|
1780
|
+
* dedicated SFTP connection.
|
|
1781
|
+
*
|
|
1782
|
+
* Two tables because the budget is per connection (`MaxSessions` counts
|
|
1783
|
+
* channels on one link): a fallback SFTP operation counts against the shared
|
|
1784
|
+
* table — the connection it really rides — and never against both.
|
|
1785
|
+
*/
|
|
1624
1786
|
slots = /* @__PURE__ */ new Map();
|
|
1787
|
+
sftpSlots = /* @__PURE__ */ new Map();
|
|
1788
|
+
/**
|
|
1789
|
+
* Channel budget of the NETWORK verbs (see {@link NET_CHANNEL_LIMIT}).
|
|
1790
|
+
*
|
|
1791
|
+
* A third table on the SHARED connection: unlike the SFTP lane this is not about
|
|
1792
|
+
* isolating bytes, it is about isolating TIME. A `git push` holds its channel
|
|
1793
|
+
* for minutes, and the failure mode it caused — a full exec table, so every
|
|
1794
|
+
* status refresh answered "host is busy" after 30 s — came from the two kinds
|
|
1795
|
+
* sharing one ceiling.
|
|
1796
|
+
*/
|
|
1797
|
+
netSlots = /* @__PURE__ */ new Map();
|
|
1798
|
+
/**
|
|
1799
|
+
* Channel budget of the GIT COMMANDS (see {@link GIT_CHANNEL_LIMIT}).
|
|
1800
|
+
*
|
|
1801
|
+
* A fourth table on the SHARED connection, for the same reason as the network
|
|
1802
|
+
* one — hold TIME, not traffic: a `git status` on a big tree may run for
|
|
1803
|
+
* minutes, and a git command must not be the reason a monitor sample or an agent
|
|
1804
|
+
* tool waits.
|
|
1805
|
+
*/
|
|
1806
|
+
gitSlots = /* @__PURE__ */ new Map();
|
|
1625
1807
|
/** Per-alias BROWSE SFTP session (see withBrowseSftp). */
|
|
1626
1808
|
browsed = /* @__PURE__ */ new Map();
|
|
1627
1809
|
/** Per-alias connection health for the browser's status chip (see status). */
|
|
@@ -1636,15 +1818,23 @@ var RemotePool = class {
|
|
|
1636
1818
|
this.evictFrom(this.conns, alias);
|
|
1637
1819
|
this.evictFrom(this.shellConns, alias);
|
|
1638
1820
|
this.evictFrom(this.forwardConns, alias);
|
|
1821
|
+
this.evictFrom(this.sftpConns, alias);
|
|
1639
1822
|
this.forwardBlocked.delete(alias);
|
|
1640
1823
|
this.forwardFallbackNoted.delete(alias);
|
|
1824
|
+
this.sftpBlocked.delete(alias);
|
|
1825
|
+
this.sftpFallbackNoted.delete(alias);
|
|
1826
|
+
this.sftpFallback.delete(alias);
|
|
1827
|
+
this.sftpSlots.delete(alias);
|
|
1828
|
+
this.netSlots.delete(alias);
|
|
1829
|
+
this.gitSlots.delete(alias);
|
|
1641
1830
|
}
|
|
1642
1831
|
/** Close every pooled connection. */
|
|
1643
1832
|
dispose() {
|
|
1644
1833
|
for (const alias of /* @__PURE__ */ new Set([
|
|
1645
1834
|
...this.conns.keys(),
|
|
1646
1835
|
...this.shellConns.keys(),
|
|
1647
|
-
...this.forwardConns.keys()
|
|
1836
|
+
...this.forwardConns.keys(),
|
|
1837
|
+
...this.sftpConns.keys()
|
|
1648
1838
|
])) this.drop(alias);
|
|
1649
1839
|
}
|
|
1650
1840
|
/**
|
|
@@ -1683,7 +1873,7 @@ var RemotePool = class {
|
|
|
1683
1873
|
return {
|
|
1684
1874
|
alias,
|
|
1685
1875
|
state: entry?.state ?? "idle",
|
|
1686
|
-
open: pooled !== void 0 && pooled.connecting === void 0 || this.shellConns.get(alias)?.connecting === void 0 && this.shellConns.has(alias) || this.forwardConns.get(alias)?.connecting === void 0 && this.forwardConns.has(alias),
|
|
1876
|
+
open: pooled !== void 0 && pooled.connecting === void 0 || this.shellConns.get(alias)?.connecting === void 0 && this.shellConns.has(alias) || this.forwardConns.get(alias)?.connecting === void 0 && this.forwardConns.has(alias) || this.sftpConns.get(alias)?.connecting === void 0 && this.sftpConns.has(alias),
|
|
1687
1877
|
...entry?.okAt === void 0 ? {} : { okAt: entry.okAt },
|
|
1688
1878
|
...entry?.errorAt === void 0 ? {} : { errorAt: entry.errorAt },
|
|
1689
1879
|
...entry?.error === void 0 ? {} : { error: entry.error },
|
|
@@ -1693,17 +1883,81 @@ var RemotePool = class {
|
|
|
1693
1883
|
});
|
|
1694
1884
|
}
|
|
1695
1885
|
/**
|
|
1886
|
+
* Live pool shape per host: connections, channels, and channel-budget
|
|
1887
|
+
* occupancy (see `RemoteHostPoolStats`).
|
|
1888
|
+
*
|
|
1889
|
+
* Same contract as `status()`: read-only and dial-free, so the monitor's poll
|
|
1890
|
+
* reports what the pool already knows instead of creating work. Answers every
|
|
1891
|
+
* alias the store knows PLUS every alias the pool holds something for — a host
|
|
1892
|
+
* removed from the store still has a connection for a moment, and a page that
|
|
1893
|
+
* shows connections must not hide exactly those.
|
|
1894
|
+
*/
|
|
1895
|
+
stats() {
|
|
1896
|
+
const aliases = /* @__PURE__ */ new Set([
|
|
1897
|
+
...this.conns.keys(),
|
|
1898
|
+
...this.shellConns.keys(),
|
|
1899
|
+
...this.forwardConns.keys(),
|
|
1900
|
+
...this.sftpConns.keys(),
|
|
1901
|
+
...this.health.keys()
|
|
1902
|
+
]);
|
|
1903
|
+
try {
|
|
1904
|
+
for (const host of this.store.list()) aliases.add(host.alias);
|
|
1905
|
+
} catch {}
|
|
1906
|
+
return [...aliases].sort().map((alias) => {
|
|
1907
|
+
const shared = this.conns.get(alias);
|
|
1908
|
+
const sharedOpen = shared !== void 0 && shared.connecting === void 0;
|
|
1909
|
+
return {
|
|
1910
|
+
alias,
|
|
1911
|
+
connections: {
|
|
1912
|
+
shared: this.laneOf(this.conns, alias),
|
|
1913
|
+
pty: this.laneOf(this.shellConns, alias),
|
|
1914
|
+
forward: this.laneOf(this.forwardConns, alias),
|
|
1915
|
+
sftp: this.laneOf(this.sftpConns, alias)
|
|
1916
|
+
},
|
|
1917
|
+
slots: this.slotsOf(this.slots.get(alias)),
|
|
1918
|
+
...this.sftpSlots.has(alias) ? { sftpSlots: this.slotsOf(this.sftpSlots.get(alias), SFTP_CHANNEL_LIMIT) } : {},
|
|
1919
|
+
...this.netSlots.has(alias) ? { netSlots: this.slotsOf(this.netSlots.get(alias), NET_CHANNEL_LIMIT) } : {},
|
|
1920
|
+
...this.gitSlots.has(alias) ? { gitSlots: this.slotsOf(this.gitSlots.get(alias), GIT_CHANNEL_LIMIT) } : {},
|
|
1921
|
+
...sharedOpen && shared.egress !== void 0 ? { egress: shared.egress } : {},
|
|
1922
|
+
...this.sftpFallback.has(alias) ? { sftpFallback: true } : {}
|
|
1923
|
+
};
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
/** One lane of one host: dialed or not, its open channels, its pinned rounds. */
|
|
1927
|
+
laneOf(map, alias) {
|
|
1928
|
+
const pooled = map.get(alias);
|
|
1929
|
+
if (pooled === void 0) return {
|
|
1930
|
+
open: false,
|
|
1931
|
+
channels: 0,
|
|
1932
|
+
held: 0
|
|
1933
|
+
};
|
|
1934
|
+
return {
|
|
1935
|
+
open: pooled.connecting === void 0,
|
|
1936
|
+
channels: pooled.channels ?? 0,
|
|
1937
|
+
held: pooled.held
|
|
1938
|
+
};
|
|
1939
|
+
}
|
|
1940
|
+
/** One channel budget, as reported (a budget nobody touched is empty, not absent). */
|
|
1941
|
+
slotsOf(state, limit = CHANNEL_LIMIT) {
|
|
1942
|
+
return {
|
|
1943
|
+
active: state?.active ?? 0,
|
|
1944
|
+
queued: state?.queue.length ?? 0,
|
|
1945
|
+
limit
|
|
1946
|
+
};
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1696
1949
|
* The egress of the alias' LIVE connection, whichever class owns it.
|
|
1697
1950
|
*
|
|
1698
|
-
* All
|
|
1699
|
-
*
|
|
1700
|
-
*
|
|
1951
|
+
* All four classes open through the same resolved route, so the first one that
|
|
1952
|
+
* is up answers for the alias; the shared connection is preferred because it is
|
|
1953
|
+
* the one exec/browse (the surface the chip describes) rides.
|
|
1701
1954
|
*/
|
|
1702
1955
|
liveEgress(alias) {
|
|
1703
1956
|
for (const map of [
|
|
1704
1957
|
this.conns,
|
|
1705
1958
|
this.forwardConns,
|
|
1706
|
-
this.shellConns
|
|
1959
|
+
this.shellConns,
|
|
1960
|
+
this.sftpConns
|
|
1707
1961
|
]) {
|
|
1708
1962
|
const pooled = map.get(alias);
|
|
1709
1963
|
if (pooled === void 0 || pooled.connecting !== void 0) continue;
|
|
@@ -1814,12 +2068,18 @@ var RemotePool = class {
|
|
|
1814
2068
|
* of queueing. The pool's own housekeeping (`bare`: the setsid probe and the
|
|
1815
2069
|
* group kill) is exempt — a kill that waits behind user traffic would be a
|
|
1816
2070
|
* worse bug than the one it fixes.
|
|
2071
|
+
*
|
|
2072
|
+
* `opts.lane` picks the budget: network verbs (`git fetch/pull/push/sync`) have
|
|
2073
|
+
* their own small one, so a 20-minute push cannot starve the exec slots.
|
|
2074
|
+
* Acquisition and release read the SAME table — a mismatch would leak a slot on
|
|
2075
|
+
* one budget and double-count on the other.
|
|
1817
2076
|
*/
|
|
1818
2077
|
async exec(alias, command, opts = {}) {
|
|
1819
2078
|
if (opts.bare === true) return this.runExec(alias, command, opts);
|
|
1820
2079
|
const started = Date.now();
|
|
2080
|
+
const slots = this.execBudget(opts.lane);
|
|
1821
2081
|
try {
|
|
1822
|
-
await this.acquireSlot(alias);
|
|
2082
|
+
await this.acquireSlot(alias, slots);
|
|
1823
2083
|
} catch (error) {
|
|
1824
2084
|
return {
|
|
1825
2085
|
success: false,
|
|
@@ -1837,9 +2097,15 @@ var RemotePool = class {
|
|
|
1837
2097
|
else if (result.success || result.exitCode !== null) this.markOk(alias);
|
|
1838
2098
|
return result;
|
|
1839
2099
|
} finally {
|
|
1840
|
-
this.releaseSlot(alias);
|
|
2100
|
+
this.releaseSlot(alias, slots);
|
|
1841
2101
|
}
|
|
1842
2102
|
}
|
|
2103
|
+
/** The channel budget an exec spends, by lane (see `ExecOptions.lane`). */
|
|
2104
|
+
execBudget(lane) {
|
|
2105
|
+
if (lane === "network") return this.netSlots;
|
|
2106
|
+
if (lane === "git") return this.gitSlots;
|
|
2107
|
+
return this.slots;
|
|
2108
|
+
}
|
|
1843
2109
|
/** The exec body, run while the caller already holds a channel slot. */
|
|
1844
2110
|
async runExec(alias, command, opts = {}) {
|
|
1845
2111
|
const started = Date.now();
|
|
@@ -1976,6 +2242,7 @@ var RemotePool = class {
|
|
|
1976
2242
|
} catch {}
|
|
1977
2243
|
}, GROUP_KILL_GRACE_MS).unref?.();
|
|
1978
2244
|
else stream = channel;
|
|
2245
|
+
this.countChannel(this.conns, alias, channel);
|
|
1979
2246
|
channel.on("close", (code, signalName) => {
|
|
1980
2247
|
if (code !== null && code !== void 0 || signalName !== null && signalName !== void 0) exited = true;
|
|
1981
2248
|
if (timedOut && session && pgid === void 0) warnOnce();
|
|
@@ -2085,6 +2352,7 @@ var RemotePool = class {
|
|
|
2085
2352
|
if (dedicated) this.releaseIn(this.shellConns, alias);
|
|
2086
2353
|
else this.release(alias);
|
|
2087
2354
|
});
|
|
2355
|
+
this.countChannel(dedicated ? this.shellConns : this.conns, alias, channel);
|
|
2088
2356
|
this.markOk(alias);
|
|
2089
2357
|
return channel;
|
|
2090
2358
|
} catch (error) {
|
|
@@ -2094,10 +2362,37 @@ var RemotePool = class {
|
|
|
2094
2362
|
throw error;
|
|
2095
2363
|
}
|
|
2096
2364
|
}
|
|
2097
|
-
/**
|
|
2365
|
+
/**
|
|
2366
|
+
* Open one exec channel. The caller owns the channel lifecycle.
|
|
2367
|
+
*
|
|
2368
|
+
* This channel spends an EXEC SLOT (same budget as `runExec`): it is a real
|
|
2369
|
+
* `conn.exec` session on the shared connection, so `MaxSessions` counts it
|
|
2370
|
+
* whether the pool does or not. The subprocess runtime opens these for every
|
|
2371
|
+
* command it runs (`p2/ssh-proc.ts`) and can have several in flight, which used
|
|
2372
|
+
* to be the one way to push the shared connection past its ceiling: sshd then
|
|
2373
|
+
* refuses the extra channel HARD — no retry, and the operation fails looking
|
|
2374
|
+
* like a broken host. A slot makes the surplus WAIT instead: bounded queue, then
|
|
2375
|
+
* a readable "busy" message naming the cause.
|
|
2376
|
+
*/
|
|
2098
2377
|
async execChannel(alias, command) {
|
|
2099
|
-
const
|
|
2378
|
+
const slots = this.slots;
|
|
2379
|
+
await this.acquireSlot(alias, slots);
|
|
2380
|
+
let conn;
|
|
2381
|
+
try {
|
|
2382
|
+
conn = await this.acquire(alias);
|
|
2383
|
+
} catch (error) {
|
|
2384
|
+
this.releaseSlot(alias, slots);
|
|
2385
|
+
throw error;
|
|
2386
|
+
}
|
|
2100
2387
|
this.hold(alias);
|
|
2388
|
+
let released = false;
|
|
2389
|
+
/** Give the slot and the hold back exactly once (close and error may both fire). */
|
|
2390
|
+
const releaseOnce = () => {
|
|
2391
|
+
if (released) return;
|
|
2392
|
+
released = true;
|
|
2393
|
+
this.release(alias);
|
|
2394
|
+
this.releaseSlot(alias, slots);
|
|
2395
|
+
};
|
|
2101
2396
|
try {
|
|
2102
2397
|
const channel = await new Promise((resolve, reject) => {
|
|
2103
2398
|
conn.exec(command, (error, stream) => {
|
|
@@ -2105,11 +2400,13 @@ var RemotePool = class {
|
|
|
2105
2400
|
else resolve(stream);
|
|
2106
2401
|
});
|
|
2107
2402
|
});
|
|
2108
|
-
channel.once("close",
|
|
2403
|
+
channel.once("close", releaseOnce);
|
|
2404
|
+
channel.once("error", releaseOnce);
|
|
2405
|
+
this.countChannel(this.conns, alias, channel);
|
|
2109
2406
|
this.markOk(alias);
|
|
2110
2407
|
return channel;
|
|
2111
2408
|
} catch (error) {
|
|
2112
|
-
|
|
2409
|
+
releaseOnce();
|
|
2113
2410
|
if (isConnectionFailure(error)) this.markError(alias, error);
|
|
2114
2411
|
throw error;
|
|
2115
2412
|
}
|
|
@@ -2193,6 +2490,7 @@ var RemotePool = class {
|
|
|
2193
2490
|
if (dedicated) this.releaseIn(this.forwardConns, alias);
|
|
2194
2491
|
else this.release(alias);
|
|
2195
2492
|
});
|
|
2493
|
+
this.countChannel(dedicated ? this.forwardConns : this.conns, alias, channel);
|
|
2196
2494
|
if (!dedicated) this.markOk(alias);
|
|
2197
2495
|
return {
|
|
2198
2496
|
channel,
|
|
@@ -2246,17 +2544,13 @@ var RemotePool = class {
|
|
|
2246
2544
|
}
|
|
2247
2545
|
/** Run one callback with a fresh SFTP session (closed afterwards). */
|
|
2248
2546
|
async withSftp(alias, fn, opts = {}) {
|
|
2249
|
-
await this.
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
this.releaseSlot(alias);
|
|
2255
|
-
throw error;
|
|
2256
|
-
}
|
|
2257
|
-
this.hold(alias);
|
|
2547
|
+
const lane = await this.acquireSftp(alias);
|
|
2548
|
+
const slots = this.laneSlots(lane.lane);
|
|
2549
|
+
const connMap = this.laneConns(lane.lane);
|
|
2550
|
+
await this.acquireSlot(alias, slots);
|
|
2551
|
+
this.holdIn(connMap, alias);
|
|
2258
2552
|
try {
|
|
2259
|
-
const sftp = await this.openSession(conn);
|
|
2553
|
+
const sftp = await this.openSession(lane.conn, alias, connMap);
|
|
2260
2554
|
try {
|
|
2261
2555
|
const limit = opts.timeoutMs ?? SFTP_OP_TIMEOUT_MS;
|
|
2262
2556
|
const result = limit <= 0 ? await fn(sftp) : await withDeadline(fn(sftp), limit, `SFTP operation timed out after ${limit / 1e3}s`);
|
|
@@ -2271,8 +2565,8 @@ var RemotePool = class {
|
|
|
2271
2565
|
this.noteOpFailure(alias, error);
|
|
2272
2566
|
throw error;
|
|
2273
2567
|
} finally {
|
|
2274
|
-
this.
|
|
2275
|
-
this.releaseSlot(alias);
|
|
2568
|
+
this.releaseIn(connMap, alias);
|
|
2569
|
+
this.releaseSlot(alias, slots);
|
|
2276
2570
|
}
|
|
2277
2571
|
}
|
|
2278
2572
|
/**
|
|
@@ -2305,24 +2599,28 @@ var RemotePool = class {
|
|
|
2305
2599
|
const sessions = [];
|
|
2306
2600
|
let slots = 0;
|
|
2307
2601
|
let holds = 0;
|
|
2602
|
+
/** Which lane the transfer got; undefined until the acquisition succeeded. */
|
|
2603
|
+
let lane;
|
|
2308
2604
|
/** Set when the callback settles: a late `addChannel` must not leak resources. */
|
|
2309
2605
|
let closed = false;
|
|
2310
|
-
let conn;
|
|
2311
2606
|
try {
|
|
2312
|
-
await this.
|
|
2607
|
+
const lease = await this.acquireSftp(alias);
|
|
2608
|
+
lane = lease.lane;
|
|
2609
|
+
const slotTable = this.laneSlots(lease.lane);
|
|
2610
|
+
const connMap = this.laneConns(lease.lane);
|
|
2611
|
+
await this.acquireSlot(alias, slotTable);
|
|
2313
2612
|
slots += 1;
|
|
2314
|
-
|
|
2315
|
-
this.hold(alias);
|
|
2613
|
+
this.holdIn(connMap, alias);
|
|
2316
2614
|
holds += 1;
|
|
2317
|
-
sessions.push(await this.openSession(conn));
|
|
2615
|
+
sessions.push(await this.openSession(lease.conn, alias, connMap));
|
|
2318
2616
|
const addChannel = async () => {
|
|
2319
2617
|
if (closed) return void 0;
|
|
2320
|
-
if (!this.tryAcquireSlot(alias)) return void 0;
|
|
2618
|
+
if (!this.tryAcquireSlot(alias, slotTable)) return void 0;
|
|
2321
2619
|
slots += 1;
|
|
2322
|
-
this.
|
|
2620
|
+
this.holdIn(connMap, alias);
|
|
2323
2621
|
holds += 1;
|
|
2324
2622
|
try {
|
|
2325
|
-
const extra = await this.openSession(conn);
|
|
2623
|
+
const extra = await this.openSession(lease.conn, alias, connMap);
|
|
2326
2624
|
if (closed) {
|
|
2327
2625
|
try {
|
|
2328
2626
|
extra.end();
|
|
@@ -2346,8 +2644,10 @@ var RemotePool = class {
|
|
|
2346
2644
|
for (const session of sessions) try {
|
|
2347
2645
|
session.end();
|
|
2348
2646
|
} catch {}
|
|
2349
|
-
|
|
2350
|
-
|
|
2647
|
+
if (lane !== void 0) {
|
|
2648
|
+
for (let i = 0; i < holds; i += 1) this.releaseIn(this.laneConns(lane), alias);
|
|
2649
|
+
for (let i = 0; i < slots; i += 1) this.releaseSlot(alias, this.laneSlots(lane));
|
|
2650
|
+
}
|
|
2351
2651
|
}
|
|
2352
2652
|
}
|
|
2353
2653
|
/**
|
|
@@ -2441,8 +2741,11 @@ var RemotePool = class {
|
|
|
2441
2741
|
* place that can still see the session, and a late one would otherwise leak a
|
|
2442
2742
|
* server-side channel that no slot or hold accounts for (the caller has long
|
|
2443
2743
|
* returned its own and moved on).
|
|
2744
|
+
*
|
|
2745
|
+
* `alias`/`map` name the connection the session rides, so the channel it holds
|
|
2746
|
+
* is counted on the right lane (see `countChannel`).
|
|
2444
2747
|
*/
|
|
2445
|
-
openSession(conn) {
|
|
2748
|
+
openSession(conn, alias, map) {
|
|
2446
2749
|
return new Promise((resolve, reject) => {
|
|
2447
2750
|
let settled = false;
|
|
2448
2751
|
const timer = setTimeout(() => {
|
|
@@ -2460,8 +2763,12 @@ var RemotePool = class {
|
|
|
2460
2763
|
}
|
|
2461
2764
|
settled = true;
|
|
2462
2765
|
clearTimeout(timer);
|
|
2463
|
-
if (error !== void 0 && error !== null)
|
|
2464
|
-
|
|
2766
|
+
if (error !== void 0 && error !== null) {
|
|
2767
|
+
reject(error);
|
|
2768
|
+
return;
|
|
2769
|
+
}
|
|
2770
|
+
this.countChannel(map, alias, session);
|
|
2771
|
+
resolve(session);
|
|
2465
2772
|
};
|
|
2466
2773
|
try {
|
|
2467
2774
|
conn.sftp((error, session) => finish(error, session));
|
|
@@ -2510,20 +2817,21 @@ var RemotePool = class {
|
|
|
2510
2817
|
/**
|
|
2511
2818
|
* Open the browse session: a channel slot + a connection hold + the SFTP
|
|
2512
2819
|
* session itself, all released together by evictBrowse().
|
|
2820
|
+
*
|
|
2821
|
+
* The lane is chosen HERE, once per session, and recorded on the state: the
|
|
2822
|
+
* eviction path must give the slot and the hold back to the connection that
|
|
2823
|
+
* actually served the session (dedicated SFTP, or the shared fallback).
|
|
2513
2824
|
*/
|
|
2514
2825
|
async openBrowse(alias, state) {
|
|
2515
|
-
await this.
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
throw error;
|
|
2522
|
-
}
|
|
2523
|
-
this.hold(alias);
|
|
2826
|
+
const lane = await this.acquireSftp(alias);
|
|
2827
|
+
state.lane = lane.lane;
|
|
2828
|
+
const slots = this.laneSlots(lane.lane);
|
|
2829
|
+
const connMap = this.laneConns(lane.lane);
|
|
2830
|
+
await this.acquireSlot(alias, slots);
|
|
2831
|
+
this.holdIn(connMap, alias);
|
|
2524
2832
|
state.leased = true;
|
|
2525
2833
|
try {
|
|
2526
|
-
const sftp = await this.openSession(conn);
|
|
2834
|
+
const sftp = await this.openSession(lane.conn, alias, connMap);
|
|
2527
2835
|
if (this.browsed.get(alias) !== state || !state.leased) {
|
|
2528
2836
|
try {
|
|
2529
2837
|
sftp.end();
|
|
@@ -2582,8 +2890,8 @@ var RemotePool = class {
|
|
|
2582
2890
|
} catch {}
|
|
2583
2891
|
if (state.leased) {
|
|
2584
2892
|
state.leased = false;
|
|
2585
|
-
this.
|
|
2586
|
-
this.releaseSlot(alias);
|
|
2893
|
+
this.releaseIn(this.laneConns(state.lane), alias);
|
|
2894
|
+
this.releaseSlot(alias, this.laneSlots(state.lane));
|
|
2587
2895
|
}
|
|
2588
2896
|
}
|
|
2589
2897
|
browseState(alias) {
|
|
@@ -2595,19 +2903,24 @@ var RemotePool = class {
|
|
|
2595
2903
|
active: 0,
|
|
2596
2904
|
stale: false,
|
|
2597
2905
|
leased: false,
|
|
2906
|
+
lane: "shared",
|
|
2598
2907
|
idle: void 0
|
|
2599
2908
|
};
|
|
2600
2909
|
this.browsed.set(alias, created);
|
|
2601
2910
|
return created;
|
|
2602
2911
|
}
|
|
2603
2912
|
/**
|
|
2604
|
-
* Take one channel slot for `alias
|
|
2605
|
-
* The wait is bounded: waiting forever behind a peer
|
|
2606
|
-
* worse than a readable error naming the cause
|
|
2913
|
+
* Take one channel slot for `alias` on the budget `slots` names, waiting FIFO
|
|
2914
|
+
* when the limit is reached. The wait is bounded: waiting forever behind a peer
|
|
2915
|
+
* that never finishes is worse than a readable error naming the cause.
|
|
2916
|
+
*
|
|
2917
|
+
* `slots` defaults to the SHARED connection's exec budget — the table every
|
|
2918
|
+
* caller meant before the lanes existed; dedicated-lane callers pass theirs.
|
|
2607
2919
|
*/
|
|
2608
|
-
async acquireSlot(alias) {
|
|
2609
|
-
const
|
|
2610
|
-
|
|
2920
|
+
async acquireSlot(alias, slots = this.slots) {
|
|
2921
|
+
const { limit, noun } = this.budgetOf(slots);
|
|
2922
|
+
const state = this.slotState(alias, slots);
|
|
2923
|
+
if (state.active < limit) {
|
|
2611
2924
|
state.active += 1;
|
|
2612
2925
|
return;
|
|
2613
2926
|
}
|
|
@@ -2623,13 +2936,41 @@ var RemotePool = class {
|
|
|
2623
2936
|
waiter.granted = true;
|
|
2624
2937
|
const index = state.queue.indexOf(waiter);
|
|
2625
2938
|
if (index >= 0) state.queue.splice(index, 1);
|
|
2626
|
-
reject(/* @__PURE__ */ new Error(`remote host ${alias} is busy: ${
|
|
2939
|
+
reject(/* @__PURE__ */ new Error(`remote host ${alias} is busy: ${limit} ${noun} already hold channels (this one waited ${CHANNEL_WAIT_MS / 1e3}s — retry once the earlier work finishes)`));
|
|
2627
2940
|
}, CHANNEL_WAIT_MS);
|
|
2628
2941
|
waiter.timer.unref?.();
|
|
2629
2942
|
state.queue.push(waiter);
|
|
2630
2943
|
});
|
|
2631
2944
|
}
|
|
2632
2945
|
/**
|
|
2946
|
+
* The ceiling and the wording of one budget.
|
|
2947
|
+
*
|
|
2948
|
+
* The TABLE is the budget's identity: a caller that hands in a table gets that
|
|
2949
|
+
* budget's own ceiling AND its own nouns, so a full network lane says "2
|
|
2950
|
+
* network operations already hold channels" and a full git lane says "2 git
|
|
2951
|
+
* commands already hold channels" instead of borrowing the exec sentence — a
|
|
2952
|
+
* user reading "5 operations" about a two-slot lane would go looking for
|
|
2953
|
+
* operations that do not exist. A new budget is described here, once.
|
|
2954
|
+
*/
|
|
2955
|
+
budgetOf(slots) {
|
|
2956
|
+
if (slots === this.netSlots) return {
|
|
2957
|
+
limit: NET_CHANNEL_LIMIT,
|
|
2958
|
+
noun: "network operations"
|
|
2959
|
+
};
|
|
2960
|
+
if (slots === this.gitSlots) return {
|
|
2961
|
+
limit: GIT_CHANNEL_LIMIT,
|
|
2962
|
+
noun: "git commands"
|
|
2963
|
+
};
|
|
2964
|
+
if (slots === this.sftpSlots) return {
|
|
2965
|
+
limit: SFTP_CHANNEL_LIMIT,
|
|
2966
|
+
noun: "operations"
|
|
2967
|
+
};
|
|
2968
|
+
return {
|
|
2969
|
+
limit: CHANNEL_LIMIT,
|
|
2970
|
+
noun: "operations"
|
|
2971
|
+
};
|
|
2972
|
+
}
|
|
2973
|
+
/**
|
|
2633
2974
|
* Take one channel slot only if one is free RIGHT NOW; never queue.
|
|
2634
2975
|
*
|
|
2635
2976
|
* Optional work (a bulk transfer's second channel) must not wait behind
|
|
@@ -2639,15 +2980,16 @@ var RemotePool = class {
|
|
|
2639
2980
|
* the attempt on purpose — it has been waiting longer, and stealing its slot
|
|
2640
2981
|
* would just move the wait.
|
|
2641
2982
|
*/
|
|
2642
|
-
tryAcquireSlot(alias) {
|
|
2643
|
-
const
|
|
2644
|
-
|
|
2983
|
+
tryAcquireSlot(alias, slots = this.slots) {
|
|
2984
|
+
const { limit } = this.budgetOf(slots);
|
|
2985
|
+
const state = this.slotState(alias, slots);
|
|
2986
|
+
if (state.active >= limit || state.queue.length > 0) return false;
|
|
2645
2987
|
state.active += 1;
|
|
2646
2988
|
return true;
|
|
2647
2989
|
}
|
|
2648
|
-
/** Give a slot back, handing it straight to the longest
|
|
2649
|
-
releaseSlot(alias) {
|
|
2650
|
-
const state =
|
|
2990
|
+
/** Give a slot back to its budget, handing it straight to the longest waiter. */
|
|
2991
|
+
releaseSlot(alias, slots = this.slots) {
|
|
2992
|
+
const state = slots.get(alias);
|
|
2651
2993
|
if (state === void 0) return;
|
|
2652
2994
|
const waiter = state.queue.shift();
|
|
2653
2995
|
if (waiter !== void 0) {
|
|
@@ -2657,16 +2999,16 @@ var RemotePool = class {
|
|
|
2657
2999
|
return;
|
|
2658
3000
|
}
|
|
2659
3001
|
if (state.active > 0) state.active -= 1;
|
|
2660
|
-
if (state.active === 0)
|
|
3002
|
+
if (state.active === 0) slots.delete(alias);
|
|
2661
3003
|
}
|
|
2662
|
-
slotState(alias) {
|
|
2663
|
-
const existing =
|
|
3004
|
+
slotState(alias, slots) {
|
|
3005
|
+
const existing = slots.get(alias);
|
|
2664
3006
|
if (existing !== void 0) return existing;
|
|
2665
3007
|
const created = {
|
|
2666
3008
|
active: 0,
|
|
2667
3009
|
queue: []
|
|
2668
3010
|
};
|
|
2669
|
-
|
|
3011
|
+
slots.set(alias, created);
|
|
2670
3012
|
return created;
|
|
2671
3013
|
}
|
|
2672
3014
|
/**
|
|
@@ -2720,6 +3062,33 @@ var RemotePool = class {
|
|
|
2720
3062
|
release(alias) {
|
|
2721
3063
|
this.releaseIn(this.conns, alias);
|
|
2722
3064
|
}
|
|
3065
|
+
/**
|
|
3066
|
+
* Count one SSH channel that is now open on a pooled connection, and give the
|
|
3067
|
+
* number back when the channel closes.
|
|
3068
|
+
*
|
|
3069
|
+
* `held` is NOT this number (see `Pooled.channels`): a jump tunnel pins the
|
|
3070
|
+
* jump host twice for its single channel, and `stats()` reports both, so the
|
|
3071
|
+
* monitor page never prints a hold count as a channel count. The close listener
|
|
3072
|
+
* mutates the entry OBJECT it was registered on, so a channel that outlives its
|
|
3073
|
+
* entry cannot decrement the counter of a successor entry.
|
|
3074
|
+
*
|
|
3075
|
+
* The entry is looked up when the channel is COUNTED, so a session that answers
|
|
3076
|
+
* pathologically late (after `drop()` and a fresh dial) is charged to the
|
|
3077
|
+
* successor entry and given back when it closes. Accepted: the window needs a
|
|
3078
|
+
* reply that outlives a host edit, the number is diagnostic, and the pair is
|
|
3079
|
+
* still symmetric (one increment, one decrement).
|
|
3080
|
+
*/
|
|
3081
|
+
countChannel(map, alias, channel) {
|
|
3082
|
+
const pooled = map.get(alias);
|
|
3083
|
+
if (pooled === void 0 || typeof channel.on !== "function") return;
|
|
3084
|
+
pooled.channels = (pooled.channels ?? 0) + 1;
|
|
3085
|
+
let counted = true;
|
|
3086
|
+
channel.on("close", () => {
|
|
3087
|
+
if (!counted) return;
|
|
3088
|
+
counted = false;
|
|
3089
|
+
if (pooled.channels > 0) pooled.channels -= 1;
|
|
3090
|
+
});
|
|
3091
|
+
}
|
|
2723
3092
|
releaseIn(map, alias) {
|
|
2724
3093
|
const pooled = map.get(alias);
|
|
2725
3094
|
if (pooled === void 0) return;
|
|
@@ -2762,6 +3131,83 @@ var RemotePool = class {
|
|
|
2762
3131
|
throw error;
|
|
2763
3132
|
}
|
|
2764
3133
|
}
|
|
3134
|
+
/**
|
|
3135
|
+
* Take the connection a FILE operation should ride: the dedicated SFTP one,
|
|
3136
|
+
* else the shared one (recorded as the fallback).
|
|
3137
|
+
*
|
|
3138
|
+
* Same three-part shape as the PTY/forward precedents, for the same reasons:
|
|
3139
|
+
*
|
|
3140
|
+
* 1. `quiet` on the dial — a dedicated connection is an optional isolation, so
|
|
3141
|
+
* the server refusing it (MaxStartups, a per-user limit) says nothing about
|
|
3142
|
+
* the shared link and must not paint the connection chip red.
|
|
3143
|
+
* 2. A short own budget ({@link SFTP_DEDICATED_BUDGET_MS}) — browsing is one
|
|
3144
|
+
* request per folder, so waiting out the 45 s connect watchdog before falling
|
|
3145
|
+
* back would freeze the explorer. The abandoned dial keeps running: if it
|
|
3146
|
+
* eventually succeeds the connection is pooled and the next request uses it.
|
|
3147
|
+
* 3. A cooldown ({@link SFTP_DEDICATED_BLOCK_MS}) — without it every browse
|
|
3148
|
+
* request would pay that budget again. A success or `drop()` clears it.
|
|
3149
|
+
*
|
|
3150
|
+
* The shared fallback's own dial failure is NOT quiet (it is a real link
|
|
3151
|
+
* failure and `acquire` reports it as one).
|
|
3152
|
+
*/
|
|
3153
|
+
async acquireSftp(alias) {
|
|
3154
|
+
if (!this.sftpCoolingDown(alias)) {
|
|
3155
|
+
const dialing = this.acquireIn(this.sftpConns, alias, true);
|
|
3156
|
+
try {
|
|
3157
|
+
const conn = await withDeadline(dialing, SFTP_DEDICATED_BUDGET_MS, `dedicated file connection to ${alias} did not settle within ${SFTP_DEDICATED_BUDGET_MS / 1e3}s`);
|
|
3158
|
+
this.sftpBlocked.delete(alias);
|
|
3159
|
+
this.sftpFallback.delete(alias);
|
|
3160
|
+
return {
|
|
3161
|
+
conn,
|
|
3162
|
+
lane: "sftp"
|
|
3163
|
+
};
|
|
3164
|
+
} catch (error) {
|
|
3165
|
+
dialing.catch(() => void 0);
|
|
3166
|
+
this.blockSftp(alias, error);
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
const conn = await this.acquire(alias);
|
|
3170
|
+
this.sftpFallback.add(alias);
|
|
3171
|
+
return {
|
|
3172
|
+
conn,
|
|
3173
|
+
lane: "shared"
|
|
3174
|
+
};
|
|
3175
|
+
}
|
|
3176
|
+
/** The pooled map behind a lane: the dedicated SFTP connection, or the shared one. */
|
|
3177
|
+
laneConns(lane) {
|
|
3178
|
+
return lane === "sftp" ? this.sftpConns : this.conns;
|
|
3179
|
+
}
|
|
3180
|
+
/** The channel budget behind a lane: each connection has its own `MaxSessions`. */
|
|
3181
|
+
laneSlots(lane) {
|
|
3182
|
+
return lane === "sftp" ? this.sftpSlots : this.slots;
|
|
3183
|
+
}
|
|
3184
|
+
/**
|
|
3185
|
+
* Is this alias's dedicated SFTP connection in its short cooldown?
|
|
3186
|
+
*
|
|
3187
|
+
* Expired entries are dropped eagerly so the map cannot grow per alias forever.
|
|
3188
|
+
*/
|
|
3189
|
+
sftpCoolingDown(alias) {
|
|
3190
|
+
const until = this.sftpBlocked.get(alias);
|
|
3191
|
+
if (until === void 0) return false;
|
|
3192
|
+
if (Date.now() >= until) {
|
|
3193
|
+
this.sftpBlocked.delete(alias);
|
|
3194
|
+
return false;
|
|
3195
|
+
}
|
|
3196
|
+
return true;
|
|
3197
|
+
}
|
|
3198
|
+
/** Park the dedicated SFTP connection for one alias and log the downgrade, once. */
|
|
3199
|
+
blockSftp(alias, error) {
|
|
3200
|
+
this.sftpBlocked.set(alias, Date.now() + SFTP_DEDICATED_BLOCK_MS);
|
|
3201
|
+
this.sftpFallback.add(alias);
|
|
3202
|
+
this.noteSftpFallback(alias, error);
|
|
3203
|
+
}
|
|
3204
|
+
/** Log one alias's file-operation downgrade to the shared connection, once. */
|
|
3205
|
+
noteSftpFallback(alias, error) {
|
|
3206
|
+
if (this.sftpFallbackNoted.has(alias)) return;
|
|
3207
|
+
this.sftpFallbackNoted.add(alias);
|
|
3208
|
+
const detail = error instanceof Error ? error.message : String(error ?? "");
|
|
3209
|
+
console.warn(`[dsh-remote-vs] dedicated file connection to ${alias} failed; using the shared one:`, detail);
|
|
3210
|
+
}
|
|
2765
3211
|
async acquireIn(map, alias, quiet = false) {
|
|
2766
3212
|
const pooled = map.get(alias);
|
|
2767
3213
|
if (pooled !== void 0 && pooled.connecting !== void 0) return pooled.connecting;
|
|
@@ -2774,7 +3220,8 @@ var RemotePool = class {
|
|
|
2774
3220
|
timer: void 0,
|
|
2775
3221
|
lastUse: Date.now(),
|
|
2776
3222
|
connecting: void 0,
|
|
2777
|
-
held: 0
|
|
3223
|
+
held: 0,
|
|
3224
|
+
channels: 0
|
|
2778
3225
|
};
|
|
2779
3226
|
map.set(alias, entry);
|
|
2780
3227
|
/**
|
|
@@ -2842,36 +3289,32 @@ var RemotePool = class {
|
|
|
2842
3289
|
return new import_lib.Client();
|
|
2843
3290
|
}
|
|
2844
3291
|
/**
|
|
2845
|
-
* Open
|
|
3292
|
+
* Open the transport `alias`'s configured route requires, WITHOUT dialing SSH.
|
|
2846
3293
|
*
|
|
2847
|
-
*
|
|
2848
|
-
*
|
|
2849
|
-
* -
|
|
2850
|
-
*
|
|
2851
|
-
*
|
|
2852
|
-
*
|
|
2853
|
-
*
|
|
2854
|
-
* close that channel and give the hold back — a leak here would pin the jump
|
|
2855
|
-
* host's connection forever.
|
|
3294
|
+
* This is the part of {@link dial} that turns a `ResolvedProxy` decision into a
|
|
3295
|
+
* socket: nothing for a direct route, a tunnel socket for a proxy, and a
|
|
3296
|
+
* `direct-tcpip` channel riding the jump host's pooled connection for a jump.
|
|
3297
|
+
* Extracted so a SECOND ssh2 dialer — p2's helper transport, which owns its own
|
|
3298
|
+
* `Client` — takes exactly the same route instead of a bare direct connection
|
|
3299
|
+
* (otherwise a host reachable only through a proxy or jump host answers exec and
|
|
3300
|
+
* SFTP but fails every file/Git fast path).
|
|
2856
3301
|
*
|
|
2857
|
-
*
|
|
2858
|
-
*
|
|
2859
|
-
*
|
|
3302
|
+
* `route` may be passed in when the caller has already resolved it and needs the
|
|
3303
|
+
* decision for its own purposes (`dial` reports the egress it settled on): the
|
|
3304
|
+
* default resolves it from the store.
|
|
2860
3305
|
*
|
|
2861
|
-
* `
|
|
2862
|
-
*
|
|
2863
|
-
*
|
|
2864
|
-
*
|
|
2865
|
-
*
|
|
3306
|
+
* The returned `release` is IDEMPOTENT and belongs to the caller: it tears down
|
|
3307
|
+
* the tunnel (destroys the proxy socket / closes the channel and gives the jump
|
|
3308
|
+
* host's hold back). It must be called exactly once by whoever owns the socket —
|
|
3309
|
+
* on failure, on close, and on dispose. A direct route has nothing to release,
|
|
3310
|
+
* so it gets a no-op.
|
|
2866
3311
|
*/
|
|
2867
|
-
async
|
|
3312
|
+
async openRoute(alias, route = this.store.resolveProxy(alias)) {
|
|
2868
3313
|
const host = this.store.require(alias);
|
|
2869
3314
|
/** The socket ssh2 will ride, when a proxy/jump built one. */
|
|
2870
3315
|
let sock;
|
|
2871
3316
|
/** Runs once: tears the tunnel down (close the channel, release the jump). */
|
|
2872
3317
|
let releaseTunnel;
|
|
2873
|
-
const route = this.store.resolveProxy(alias);
|
|
2874
|
-
const egress = egressOf(route, host);
|
|
2875
3318
|
if (route.kind === "proxy") {
|
|
2876
3319
|
try {
|
|
2877
3320
|
sock = await openProxySocket(route.proxy, host.host, host.port);
|
|
@@ -2909,6 +3352,62 @@ var RemotePool = class {
|
|
|
2909
3352
|
};
|
|
2910
3353
|
sock = channel;
|
|
2911
3354
|
}
|
|
3355
|
+
return {
|
|
3356
|
+
...sock === void 0 ? {} : { sock },
|
|
3357
|
+
release: releaseTunnel ?? (() => {})
|
|
3358
|
+
};
|
|
3359
|
+
}
|
|
3360
|
+
/**
|
|
3361
|
+
* The routed transport for `alias`, for a caller that dials its OWN ssh2 client.
|
|
3362
|
+
*
|
|
3363
|
+
* Same decision, same error layering (`proxy auth failed: …`, `jump host "x"
|
|
3364
|
+
* failed: …`), same socket as a pooled connection would ride — p2's helper
|
|
3365
|
+
* transport uses this so "reachable only through the jump host" stops meaning
|
|
3366
|
+
* "exec works, files do not".
|
|
3367
|
+
*
|
|
3368
|
+
* **The caller owns the result**: when `sock` is set, ssh2 must be given `{ sock }`
|
|
3369
|
+
* and NOT `host`/`port` (both together let ssh2 choose between dialing the target
|
|
3370
|
+
* itself and riding the tunnel). `release` must be called exactly once — on
|
|
3371
|
+
* failure, on the connection's close, and on dispose — or the jump host's hold
|
|
3372
|
+
* leaks and the proxy socket stays open.
|
|
3373
|
+
*/
|
|
3374
|
+
async openRoutedSocket(alias) {
|
|
3375
|
+
return this.openRoute(alias);
|
|
3376
|
+
}
|
|
3377
|
+
/**
|
|
3378
|
+
* Open one SSH connection, through whatever route the store resolves.
|
|
3379
|
+
*
|
|
3380
|
+
* Three routes, and only the first is the pre-proxy code path:
|
|
3381
|
+
* - `direct`: unchanged (`host` / `port` in the connect config);
|
|
3382
|
+
* - `proxy`: the proxy module builds the tunnel and ssh2 rides the returned
|
|
3383
|
+
* socket (`sock`), so `host`/`port` are deliberately omitted (ssh2 ignores
|
|
3384
|
+
* them when `sock` is present);
|
|
3385
|
+
* - `jump`: the tunnel is a `direct-tcpip` channel on the JUMP host's pooled
|
|
3386
|
+
* connection, held for as long as the client lives. Its close/error handlers
|
|
3387
|
+
* close that channel and give the hold back — a leak here would pin the jump
|
|
3388
|
+
* host's connection forever.
|
|
3389
|
+
*
|
|
3390
|
+
* Every failure is a DIAL failure, so it keeps the existing `markError`
|
|
3391
|
+
* semantics; only the message is layered, so the user can tell which hop broke:
|
|
3392
|
+
* `proxy auth failed: …`, `proxy refused: …`, `jump host "x" failed: …`.
|
|
3393
|
+
*
|
|
3394
|
+
* `report` receives the route this dial ACTUALLY settled on, once and only on
|
|
3395
|
+
* success — a caller that asked "did this go through the jump host?" must not
|
|
3396
|
+
* be answered from a configuration that may have changed since. It is a
|
|
3397
|
+
* callback rather than a wider return type so the seam keeps returning the
|
|
3398
|
+
* `Client` the existing callers (and gates) hand around.
|
|
3399
|
+
*/
|
|
3400
|
+
async dial(alias, report) {
|
|
3401
|
+
const host = this.store.require(alias);
|
|
3402
|
+
const route = this.store.resolveProxy(alias);
|
|
3403
|
+
const egress = egressOf(route, host);
|
|
3404
|
+
/**
|
|
3405
|
+
* The transport this connection rides. Resolved with the SAME route the egress
|
|
3406
|
+
* was computed from, so the reported hop and the socket can never disagree —
|
|
3407
|
+
* and `openRoute` builds it exactly the way this method used to inline
|
|
3408
|
+
* (proxy tunnel socket / jump-host `direct-tcpip` channel / nothing at all).
|
|
3409
|
+
*/
|
|
3410
|
+
const routed = await this.openRoute(alias, route);
|
|
2912
3411
|
return new Promise((resolve, reject) => {
|
|
2913
3412
|
const conn = this.makeClient();
|
|
2914
3413
|
let settled = false;
|
|
@@ -2920,7 +3419,7 @@ var RemotePool = class {
|
|
|
2920
3419
|
try {
|
|
2921
3420
|
conn.end();
|
|
2922
3421
|
} catch {}
|
|
2923
|
-
|
|
3422
|
+
routed.release();
|
|
2924
3423
|
reject(explainAuthError(host, error));
|
|
2925
3424
|
};
|
|
2926
3425
|
conn.on("ready", () => {
|
|
@@ -2939,10 +3438,8 @@ var RemotePool = class {
|
|
|
2939
3438
|
* leave this promise pending forever while the caller held a channel slot.
|
|
2940
3439
|
*/
|
|
2941
3440
|
conn.on("close", () => fail(/* @__PURE__ */ new Error("connection closed during SSH handshake")));
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
conn.once("error", () => releaseTunnel?.());
|
|
2945
|
-
}
|
|
3441
|
+
conn.once("close", () => routed.release());
|
|
3442
|
+
conn.once("error", () => routed.release());
|
|
2946
3443
|
guard = setTimeout(() => {
|
|
2947
3444
|
fail(/* @__PURE__ */ new Error(`SSH handshake did not settle within ${(READY_TIMEOUT_MS + DIAL_GUARD_MARGIN_MS) / 1e3}s`));
|
|
2948
3445
|
}, READY_TIMEOUT_MS + DIAL_GUARD_MARGIN_MS);
|
|
@@ -2968,10 +3465,10 @@ var RemotePool = class {
|
|
|
2968
3465
|
return true;
|
|
2969
3466
|
};
|
|
2970
3467
|
try {
|
|
2971
|
-
const transport = sock === void 0 ? {
|
|
3468
|
+
const transport = routed.sock === void 0 ? {
|
|
2972
3469
|
host: host.host,
|
|
2973
3470
|
port: host.port
|
|
2974
|
-
} : { sock };
|
|
3471
|
+
} : { sock: routed.sock };
|
|
2975
3472
|
if (host.auth.kind === "key") {
|
|
2976
3473
|
const keyPath = host.auth.keyPath;
|
|
2977
3474
|
conn.connect({
|
|
@@ -5292,6 +5789,24 @@ function bodyAliasOf(value) {
|
|
|
5292
5789
|
return typeof alias === "string" && alias !== "" ? alias : null;
|
|
5293
5790
|
}
|
|
5294
5791
|
/**
|
|
5792
|
+
* Which channel budget an exec request asks for, from the two accepted spellings.
|
|
5793
|
+
*
|
|
5794
|
+
* `lane` is the current field (`'network'` for fetch/pull/push, `'git'` for git
|
|
5795
|
+
* commands — reads and local writes alike) and `network: true` is the flag from
|
|
5796
|
+
* before the git budget existed — kept because a browser tab can run an OLD client
|
|
5797
|
+
* bundle against this host. An explicit `lane` wins when both are sent, and
|
|
5798
|
+
* anything else (a typo, `'exec'`, a number) is IGNORED so a bad value can never
|
|
5799
|
+
* move traffic onto a smaller budget than the caller meant.
|
|
5800
|
+
*
|
|
5801
|
+
* An empty object means "no lane": the command spends the shared exec budget,
|
|
5802
|
+
* which is what every caller that predates the lanes gets.
|
|
5803
|
+
*/
|
|
5804
|
+
function execLaneOf(body) {
|
|
5805
|
+
if (body.lane === "network" || body.lane === "git") return { lane: body.lane };
|
|
5806
|
+
if (body.network === true) return { lane: "network" };
|
|
5807
|
+
return {};
|
|
5808
|
+
}
|
|
5809
|
+
/**
|
|
5295
5810
|
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
5296
5811
|
*
|
|
5297
5812
|
* `/files` used to throw plain Errors for missing fields and map EVERY failure
|
|
@@ -5767,6 +6282,30 @@ function makeRoutes(deps) {
|
|
|
5767
6282
|
writeJson(res, 200, { connections: pool.status() });
|
|
5768
6283
|
}
|
|
5769
6284
|
},
|
|
6285
|
+
{
|
|
6286
|
+
kind: "exact",
|
|
6287
|
+
path: REMOTE_API.connections,
|
|
6288
|
+
handler: async (req, res) => {
|
|
6289
|
+
if (!isLoopbackRequest(req)) {
|
|
6290
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
6291
|
+
return;
|
|
6292
|
+
}
|
|
6293
|
+
if (req.method !== "GET") {
|
|
6294
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
6295
|
+
return;
|
|
6296
|
+
}
|
|
6297
|
+
try {
|
|
6298
|
+
const hosts = pool.stats();
|
|
6299
|
+
await Promise.all(hosts.map(async (host) => {
|
|
6300
|
+
const lane = await worlds.helperLane(host.alias).catch(() => void 0);
|
|
6301
|
+
if (lane !== void 0) host.connections.helper = lane;
|
|
6302
|
+
}));
|
|
6303
|
+
writeJson(res, 200, { hosts });
|
|
6304
|
+
} catch (error) {
|
|
6305
|
+
writeRouteError(res, error);
|
|
6306
|
+
}
|
|
6307
|
+
}
|
|
6308
|
+
},
|
|
5770
6309
|
{
|
|
5771
6310
|
kind: "exact",
|
|
5772
6311
|
path: REMOTE_API.test,
|
|
@@ -5836,6 +6375,7 @@ function makeRoutes(deps) {
|
|
|
5836
6375
|
return;
|
|
5837
6376
|
}
|
|
5838
6377
|
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
6378
|
+
const lane = execLaneOf(body);
|
|
5839
6379
|
/** The client closed the request: its response is never read. */
|
|
5840
6380
|
let clientGone = false;
|
|
5841
6381
|
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
@@ -5866,6 +6406,7 @@ function makeRoutes(deps) {
|
|
|
5866
6406
|
try {
|
|
5867
6407
|
const result = await pool.exec(body.alias, body.command, {
|
|
5868
6408
|
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
6409
|
+
...lane,
|
|
5869
6410
|
onCancel: (handle) => {
|
|
5870
6411
|
cancel = handle;
|
|
5871
6412
|
if (cancelRequested && !cancelSent) {
|
|
@@ -5900,6 +6441,7 @@ function makeRoutes(deps) {
|
|
|
5900
6441
|
return;
|
|
5901
6442
|
}
|
|
5902
6443
|
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
6444
|
+
const lane = execLaneOf(body);
|
|
5903
6445
|
res.writeHead(200, {
|
|
5904
6446
|
"Content-Type": "text/event-stream; charset=utf-8",
|
|
5905
6447
|
"Cache-Control": "no-cache, no-transform",
|
|
@@ -5968,6 +6510,7 @@ function makeRoutes(deps) {
|
|
|
5968
6510
|
try {
|
|
5969
6511
|
const result = await pool.exec(body.alias, body.command, {
|
|
5970
6512
|
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
6513
|
+
...lane,
|
|
5971
6514
|
onData: (stream, text) => {
|
|
5972
6515
|
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
5973
6516
|
sent += Buffer.byteLength(text, "utf8");
|
|
@@ -7153,13 +7696,24 @@ function excludeDirNames(dirs) {
|
|
|
7153
7696
|
/**
|
|
7154
7697
|
* New files git is not tracking yet (the ones a session usually creates).
|
|
7155
7698
|
*
|
|
7699
|
+
* `--directory` is the load-bearing flag. Without it git descends into every
|
|
7700
|
+
* untracked directory and prints one path per FILE: measured on the reference
|
|
7701
|
+
* monorepo that is 11.25s warm, and on a cold AOSP checkout past a minute. With
|
|
7702
|
+
* it, an untracked directory is reported ONCE as `dir/` (trailing slash) and git
|
|
7703
|
+
* stops descending — a build tree or an un-ignored dependency tree costs one
|
|
7704
|
+
* record instead of thousands of `stat`s. The scan still has to walk the tracked
|
|
7705
|
+
* part of the tree, but the expensive half is exactly the part that is skipped.
|
|
7706
|
+
*
|
|
7707
|
+
* The semantic cost is that the answer now names directories; the caller expands
|
|
7708
|
+
* the ones it still wants files for with {@link untrackedExpandCommand}, within
|
|
7709
|
+
* bounds of its own (see `UNTRACKED_EXPAND_DIRS` in `recorder.ts`). A file that
|
|
7710
|
+
* git reports directly is already the final answer.
|
|
7711
|
+
*
|
|
7156
7712
|
* The exclusions are pushed down as pathspecs (`:(exclude)out`) rather than
|
|
7157
7713
|
* filtered after the fact, so those paths never cross the wire and never enter
|
|
7158
7714
|
* the 500-row cap. Measured on a tree with 3000 build outputs + 300 sources:
|
|
7159
7715
|
* 3300 records / 61.5 KB unfiltered versus 300 records / 6.8 KB with two
|
|
7160
|
-
* exclusions
|
|
7161
|
-
* those directories), so the win is wire bytes, parse time and the read budget,
|
|
7162
|
-
* not scan latency.
|
|
7716
|
+
* exclusions.
|
|
7163
7717
|
*
|
|
7164
7718
|
* @param root - remote absolute POSIX directory (the session cwd).
|
|
7165
7719
|
* @param excludeDirs - directory names to leave out (see {@link excludeDirNames}).
|
|
@@ -7171,7 +7725,49 @@ function untrackedCommand(root, excludeDirs = []) {
|
|
|
7171
7725
|
".",
|
|
7172
7726
|
...excludeDirNames(excludeDirs).map((name) => shq(`:(exclude)${name}`))
|
|
7173
7727
|
].join(" ");
|
|
7174
|
-
return [cdLine(root), `git ls-files --others --exclude-standard -z ${pathspec}`].join("\n");
|
|
7728
|
+
return [cdLine(root), `git ls-files --others --exclude-standard -z --directory ${pathspec}`].join("\n");
|
|
7729
|
+
}
|
|
7730
|
+
/**
|
|
7731
|
+
* Whether a `--directory` record names a collapsed DIRECTORY.
|
|
7732
|
+
*
|
|
7733
|
+
* git marks one with a trailing slash; a record without it is a file, which is
|
|
7734
|
+
* already the final answer and must not be expanded again.
|
|
7735
|
+
*/
|
|
7736
|
+
function isUntrackedDirectoryPath(path) {
|
|
7737
|
+
return path.endsWith("/");
|
|
7738
|
+
}
|
|
7739
|
+
/**
|
|
7740
|
+
* List the files inside EVERY directory a {@link untrackedCommand} walk
|
|
7741
|
+
* collapsed, in ONE command.
|
|
7742
|
+
*
|
|
7743
|
+
* Same walk, same exclusions, minus `--directory`: the caller gets file paths
|
|
7744
|
+
* again. One command for all of them on purpose — git also collapses directories
|
|
7745
|
+
* that hold nothing but IGNORED files (`build/` full of gitignored output) and
|
|
7746
|
+
* empty ones, so a per-directory loop would spend a whole round trip (and one of
|
|
7747
|
+
* the expansion slots) proving that such a directory has no visible file inside.
|
|
7748
|
+
* Batched, an empty directory costs nothing and cannot push a real one out of
|
|
7749
|
+
* the slot budget.
|
|
7750
|
+
*
|
|
7751
|
+
* Every directory comes from git's own output, but each still crosses a shell, so
|
|
7752
|
+
* each goes through {@link shq} — a directory named `it's a 目录` is one quoted
|
|
7753
|
+
* argument, never a second command or a flag.
|
|
7754
|
+
*
|
|
7755
|
+
* The trailing slash the fast walk adds is dropped: the pathspec names the
|
|
7756
|
+
* directory either way, and a bare path keeps this builder from ever emitting
|
|
7757
|
+
* `-- ''` (which would mean "the whole repository").
|
|
7758
|
+
*
|
|
7759
|
+
* @param root - remote absolute POSIX directory (the session cwd).
|
|
7760
|
+
* @param dirs - collapsed directories from {@link untrackedCommand}, with or
|
|
7761
|
+
* without their trailing slashes.
|
|
7762
|
+
* @param excludeDirs - the same exclusions the fast walk used (nested build
|
|
7763
|
+
* output inside a collapsed directory must stay out too).
|
|
7764
|
+
*/
|
|
7765
|
+
function untrackedExpandCommand(root, dirs, excludeDirs = []) {
|
|
7766
|
+
const targets = dirs.map((dir) => dir.replace(/\/+$/, "")).filter((dir) => dir !== "");
|
|
7767
|
+
if (targets.length === 0) throw new Error("untracked expand needs at least one directory path");
|
|
7768
|
+
const excludes = excludeDirNames(excludeDirs).map((name) => shq(`:(exclude)${name}`));
|
|
7769
|
+
const pathspec = [...targets.map((dir) => shq(dir)), ...excludes].join(" ");
|
|
7770
|
+
return [cdLine(root), `git ls-files --others --exclude-standard -z -- ${pathspec}`].join("\n");
|
|
7175
7771
|
}
|
|
7176
7772
|
/**
|
|
7177
7773
|
* Parse `git status --porcelain -z -uno` output.
|
|
@@ -7860,8 +8456,6 @@ function filePatch(displayPath, hunks) {
|
|
|
7860
8456
|
}
|
|
7861
8457
|
return `${out.join("\n")}\n`;
|
|
7862
8458
|
}
|
|
7863
|
-
//#endregion
|
|
7864
|
-
//#region src/changes/recorder.ts
|
|
7865
8459
|
/**
|
|
7866
8460
|
* Map a scan entry onto the panel's vocabulary.
|
|
7867
8461
|
*
|
|
@@ -8377,6 +8971,7 @@ var SessionChanges = class {
|
|
|
8377
8971
|
ledger.adoptedSubdir = false;
|
|
8378
8972
|
ledger.tracked = [];
|
|
8379
8973
|
ledger.untracked = void 0;
|
|
8974
|
+
ledger.untrackedTruncated = false;
|
|
8380
8975
|
ledger.untrackedAt = 0;
|
|
8381
8976
|
ledger.untrackedMs = void 0;
|
|
8382
8977
|
ledger.scanMs = void 0;
|
|
@@ -8458,6 +9053,7 @@ var SessionChanges = class {
|
|
|
8458
9053
|
subdir: "",
|
|
8459
9054
|
tracked: [],
|
|
8460
9055
|
untracked: void 0,
|
|
9056
|
+
untrackedTruncated: false,
|
|
8461
9057
|
untrackedAt: 0,
|
|
8462
9058
|
untrackedMs: void 0,
|
|
8463
9059
|
scanMs: void 0,
|
|
@@ -8541,7 +9137,7 @@ var SessionChanges = class {
|
|
|
8541
9137
|
const entries = [...merged.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
8542
9138
|
const cap = this.deps.maxFiles ?? 500;
|
|
8543
9139
|
const listed = entries.slice(0, cap);
|
|
8544
|
-
const truncated = entries.length > cap;
|
|
9140
|
+
const truncated = entries.length > cap || ledger.untrackedTruncated;
|
|
8545
9141
|
if (!ledger.baselineTaken) {
|
|
8546
9142
|
const first = await this.readPaths(ledger, listed.map((entry) => entry.path), "worktree");
|
|
8547
9143
|
if (ledger.disposed || ledger.root !== root) return;
|
|
@@ -8984,26 +9580,39 @@ var SessionChanges = class {
|
|
|
8984
9580
|
* runs twice at once, and only runs again after {@link untrackedCadence}. A
|
|
8985
9581
|
* repository that cannot answer inside {@link untrackedBudget} is written off
|
|
8986
9582
|
* for the session (`untrackedGivenUp`) rather than retried every turn.
|
|
9583
|
+
*
|
|
9584
|
+
* Two phases since the scan learned `--directory`: one fast walk that collapses
|
|
9585
|
+
* untracked directories (`build/` instead of 3000 paths) and then a BOUNDED
|
|
9586
|
+
* expansion of the collapsed entries, because the panel's contract is a list of
|
|
9587
|
+
* FILES. A walk that already answered with files expands nothing at all.
|
|
8987
9588
|
*/
|
|
8988
9589
|
async kickUntracked(ledger) {
|
|
8989
9590
|
if (ledger.disposed || ledger.untrackedGivenUp || ledger.untrackedJob !== void 0) return;
|
|
8990
9591
|
if (ledger.untracked !== void 0 && Date.now() - ledger.untrackedAt < this.untrackedCadence(ledger)) return;
|
|
8991
9592
|
const root = ledger.root;
|
|
8992
9593
|
ledger.untrackedAt = Date.now();
|
|
9594
|
+
const deadline = ledger.untrackedAt + this.untrackedBudget();
|
|
8993
9595
|
ledger.untrackedJob = (async () => {
|
|
8994
9596
|
const started = Date.now();
|
|
8995
|
-
const
|
|
8996
|
-
ledger.
|
|
9597
|
+
const excluded = this.excludedDirs();
|
|
9598
|
+
const scan = await this.run(ledger.alias, untrackedCommand(root, excluded), Math.max(1, deadline - Date.now()));
|
|
8997
9599
|
if (ledger.disposed) return;
|
|
8998
9600
|
if (ledger.root !== root) return;
|
|
8999
9601
|
if (!scan.ok) {
|
|
9602
|
+
ledger.untrackedMs = Date.now() - started;
|
|
9000
9603
|
ledger.untrackedGivenUp = true;
|
|
9604
|
+
ledger.untrackedTruncated = false;
|
|
9001
9605
|
ledger.untrackedFailure = scan.reason;
|
|
9002
9606
|
this.warn(`untracked scan unusable (${scan.reason}); new files will not be listed`);
|
|
9003
9607
|
return;
|
|
9004
9608
|
}
|
|
9005
9609
|
ledger.untrackedFailure = void 0;
|
|
9006
|
-
|
|
9610
|
+
const expanded = await this.expandUntracked(ledger, root, parseUntrackedZ(scan.stdout), excluded, deadline);
|
|
9611
|
+
if (ledger.disposed) return;
|
|
9612
|
+
if (ledger.root !== root) return;
|
|
9613
|
+
ledger.untrackedMs = Date.now() - started;
|
|
9614
|
+
ledger.untracked = expanded.entries;
|
|
9615
|
+
ledger.untrackedTruncated = expanded.truncated;
|
|
9007
9616
|
await this.absorbPreExisting(ledger);
|
|
9008
9617
|
await this.publish(ledger, true);
|
|
9009
9618
|
})().catch((error) => this.warn(`untracked scan failed: ${messageOf(error)}`)).finally(() => {
|
|
@@ -9011,6 +9620,73 @@ var SessionChanges = class {
|
|
|
9011
9620
|
});
|
|
9012
9621
|
}
|
|
9013
9622
|
/**
|
|
9623
|
+
* Turn the fast walk's collapsed directories back into files, within bounds.
|
|
9624
|
+
*
|
|
9625
|
+
* Bounded THREE ways, because every expansion is another `git ls-files` on the
|
|
9626
|
+
* same shared connection and the scan runs in the background where nobody is
|
|
9627
|
+
* watching:
|
|
9628
|
+
*
|
|
9629
|
+
* - at most {@link UNTRACKED_EXPAND_DIRS} directories, sorted first so the
|
|
9630
|
+
* choice is deterministic (a rescan of an unchanged tree expands the same
|
|
9631
|
+
* ones, and the gate can pin exactly which);
|
|
9632
|
+
* - the same file cap the panel lists (`maxFiles`, 500 by default), so one
|
|
9633
|
+
* huge directory cannot push the list past what is displayable;
|
|
9634
|
+
* - the fast walk's own deadline: when it runs out the phase stops.
|
|
9635
|
+
*
|
|
9636
|
+
* Hitting any of them — or an expansion that fails — marks the result
|
|
9637
|
+
* `truncated` instead of returning a short list silently. A failed expansion is
|
|
9638
|
+
* NOT `untrackedGivenUp`: the fast walk answered, so new-file detection still
|
|
9639
|
+
* works; only part of it is missing.
|
|
9640
|
+
*
|
|
9641
|
+
* @returns the file entries (walk files + expanded ones) and whether the list
|
|
9642
|
+
* is incomplete.
|
|
9643
|
+
*/
|
|
9644
|
+
async expandUntracked(ledger, root, walked, excludeDirs, deadline) {
|
|
9645
|
+
const entries = walked.filter((entry) => !isUntrackedDirectoryPath(entry.path));
|
|
9646
|
+
const dirs = walked.filter((entry) => isUntrackedDirectoryPath(entry.path)).map((entry) => entry.path.replace(/\/+$/, "")).filter((path) => path !== "").sort();
|
|
9647
|
+
if (dirs.length === 0) return {
|
|
9648
|
+
entries,
|
|
9649
|
+
truncated: false
|
|
9650
|
+
};
|
|
9651
|
+
const cap = this.deps.maxFiles ?? 500;
|
|
9652
|
+
let truncated = dirs.length > 20;
|
|
9653
|
+
const pending = dirs.slice(0, 20);
|
|
9654
|
+
const remaining = deadline - Date.now();
|
|
9655
|
+
if (remaining <= 0) {
|
|
9656
|
+
this.warn(`untracked expansion ran out of its budget before asking about ${pending.length} directories`);
|
|
9657
|
+
return {
|
|
9658
|
+
entries,
|
|
9659
|
+
truncated: true
|
|
9660
|
+
};
|
|
9661
|
+
}
|
|
9662
|
+
const result = await this.run(ledger.alias, untrackedExpandCommand(root, pending, excludeDirs), remaining);
|
|
9663
|
+
if (ledger.disposed || ledger.root !== root) return {
|
|
9664
|
+
entries,
|
|
9665
|
+
truncated: true
|
|
9666
|
+
};
|
|
9667
|
+
if (!result.ok) {
|
|
9668
|
+
this.warn(`expanding ${pending.length} untracked directories failed (${result.reason}); new files under them are not listed`);
|
|
9669
|
+
return {
|
|
9670
|
+
entries,
|
|
9671
|
+
truncated: true
|
|
9672
|
+
};
|
|
9673
|
+
}
|
|
9674
|
+
const seen = new Set(entries.map((entry) => entry.path));
|
|
9675
|
+
for (const entry of parseUntrackedZ(result.stdout)) {
|
|
9676
|
+
if (seen.has(entry.path)) continue;
|
|
9677
|
+
seen.add(entry.path);
|
|
9678
|
+
entries.push(entry);
|
|
9679
|
+
if (entries.length >= cap) {
|
|
9680
|
+
truncated = true;
|
|
9681
|
+
break;
|
|
9682
|
+
}
|
|
9683
|
+
}
|
|
9684
|
+
return {
|
|
9685
|
+
entries,
|
|
9686
|
+
truncated
|
|
9687
|
+
};
|
|
9688
|
+
}
|
|
9689
|
+
/**
|
|
9014
9690
|
* How long before the untracked scan may run again.
|
|
9015
9691
|
*
|
|
9016
9692
|
* Self-tuning on purpose: the same command costs ~0.2s in a small repository
|
|
@@ -9086,10 +9762,23 @@ var SessionChanges = class {
|
|
|
9086
9762
|
}
|
|
9087
9763
|
return parseReads(result.stdout);
|
|
9088
9764
|
}
|
|
9089
|
-
/**
|
|
9765
|
+
/**
|
|
9766
|
+
* Run one command on the host, normalizing failure into a reason string.
|
|
9767
|
+
*
|
|
9768
|
+
* Every command this ledger issues is a git command (a repository read: `git
|
|
9769
|
+
* status`, the untracked walk, its expansion, the file-content reads — the
|
|
9770
|
+
* recorder never mutates). So they all spend the host's git-command budget
|
|
9771
|
+
* (`lane: 'git'`) instead of a shared exec slot: these scans run in the HOST
|
|
9772
|
+
* process while the user is working, and a 300-second `git status` must not be
|
|
9773
|
+
* the reason a monitor sample, the process panel or an agent tool answers
|
|
9774
|
+
* "host is busy".
|
|
9775
|
+
*/
|
|
9090
9776
|
async run(alias, command, budget = this.execBudget()) {
|
|
9091
9777
|
try {
|
|
9092
|
-
const call = this.deps.pool.exec(alias, command, {
|
|
9778
|
+
const call = this.deps.pool.exec(alias, command, {
|
|
9779
|
+
timeoutMs: budget,
|
|
9780
|
+
lane: "git"
|
|
9781
|
+
});
|
|
9093
9782
|
const result = await this.withWatchdog(call, budget + Math.min(WATCHDOG_GRACE_MS, budget));
|
|
9094
9783
|
if (result === WATCHDOG_FIRED) return {
|
|
9095
9784
|
ok: false,
|
|
@@ -9126,19 +9815,24 @@ var SessionChanges = class {
|
|
|
9126
9815
|
if (override !== void 0 && Number.isFinite(override) && override > 0) return override;
|
|
9127
9816
|
return this.budget(this.deps.timeouts().gitMs, 5e3, 6e4);
|
|
9128
9817
|
}
|
|
9129
|
-
/** The untracked scan gets its own, more generous budget. */
|
|
9130
9818
|
/**
|
|
9131
|
-
* The untracked scan gets its own,
|
|
9132
|
-
*
|
|
9133
|
-
*
|
|
9134
|
-
*
|
|
9135
|
-
*
|
|
9136
|
-
*
|
|
9137
|
-
*
|
|
9138
|
-
*
|
|
9819
|
+
* The untracked scan gets its own, more generous budget — one minute.
|
|
9820
|
+
*
|
|
9821
|
+
* The walk is proportional to the whole checkout, not to the session, so it
|
|
9822
|
+
* cannot use the ordinary git budget. It used to be three minutes; that ceiling
|
|
9823
|
+
* was set before the scan learned `--directory`, when a monorepo's answer really
|
|
9824
|
+
* could take that long. Now the fast walk collapses whole directories, so a
|
|
9825
|
+
* minute is already several times the useful wait, and every second past it is
|
|
9826
|
+
* spent holding an exec channel on the SHARED connection while the tree is
|
|
9827
|
+
* still being walked for nothing. Past a minute the honest answer is "this host
|
|
9828
|
+
* cannot enumerate new files quickly enough" — `untrackedFailure` says so and the
|
|
9829
|
+
* panel offers a retry, which is the same contract as before, just cut shorter.
|
|
9830
|
+
*
|
|
9831
|
+
* The floor (10s) is untouched: a small repository must never be cut off by a
|
|
9832
|
+
* tiny configured `gitMs`.
|
|
9139
9833
|
*/
|
|
9140
9834
|
untrackedBudget() {
|
|
9141
|
-
return this.deps.untrackedBudgetMs ?? this.budget(this.deps.timeouts().gitMs, 1e4,
|
|
9835
|
+
return this.deps.untrackedBudgetMs ?? this.budget(this.deps.timeouts().gitMs, 1e4, 6e4);
|
|
9142
9836
|
}
|
|
9143
9837
|
/**
|
|
9144
9838
|
* The exclusions this ledger scans with, normalized once per call.
|