@agentrq/acp-gateway 0.2.19 → 0.2.21
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.md +18 -2
- package/dist/__tests__/agentInstall.test.js +187 -3
- package/dist/__tests__/agentInstall.test.js.map +1 -1
- package/dist/__tests__/config.test.js +92 -1
- package/dist/__tests__/config.test.js.map +1 -1
- package/dist/__tests__/index.test.js +627 -43
- package/dist/__tests__/index.test.js.map +1 -1
- package/dist/__tests__/progress.test.js +256 -0
- package/dist/__tests__/progress.test.js.map +1 -0
- package/dist/agentInstall.js +79 -5
- package/dist/agentInstall.js.map +1 -1
- package/dist/config.js +119 -21
- package/dist/config.js.map +1 -1
- package/dist/index.js +308 -87
- package/dist/index.js.map +1 -1
- package/dist/mcpClient.js +1 -1
- package/dist/progress.js +149 -0
- package/dist/progress.js.map +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1169,8 +1169,14 @@ async function drainConcurrencyReports(bridge, queue) {
|
|
|
1169
1169
|
* only ever runs later, from inside the queue's own methods, by which time the
|
|
1170
1170
|
* binding exists.
|
|
1171
1171
|
*/
|
|
1172
|
-
export function createTaskQueue(maxConcurrency, bridge
|
|
1173
|
-
|
|
1172
|
+
export function createTaskQueue(maxConcurrency, bridge,
|
|
1173
|
+
/**
|
|
1174
|
+
* Told when the queue may have room for another task, so the caller can go
|
|
1175
|
+
* and fetch one. Absent where nothing is fetching — the auth commands, and
|
|
1176
|
+
* tests of the reporting on its own.
|
|
1177
|
+
*/
|
|
1178
|
+
onCapacity) {
|
|
1179
|
+
const queue = new TaskQueue(maxConcurrency, () => void reportConcurrency(bridge, queue), onCapacity);
|
|
1174
1180
|
return queue;
|
|
1175
1181
|
}
|
|
1176
1182
|
/**
|
|
@@ -1353,6 +1359,7 @@ export function createAcpSessionSwitcher(connection, params, initialSessionId) {
|
|
|
1353
1359
|
export class TaskQueue {
|
|
1354
1360
|
maxConcurrency;
|
|
1355
1361
|
onChange;
|
|
1362
|
+
onCapacity;
|
|
1356
1363
|
activeTasks = 0;
|
|
1357
1364
|
queue = [];
|
|
1358
1365
|
/**
|
|
@@ -1362,9 +1369,10 @@ export class TaskQueue {
|
|
|
1362
1369
|
* interface is the fact that its numbers moved, not the delivery of that
|
|
1363
1370
|
* fact. Absent only in tests, matching `handleSetConcurrency`'s `bridge?`.
|
|
1364
1371
|
*/
|
|
1365
|
-
constructor(maxConcurrency, onChange) {
|
|
1372
|
+
constructor(maxConcurrency, onChange, onCapacity) {
|
|
1366
1373
|
this.maxConcurrency = maxConcurrency;
|
|
1367
1374
|
this.onChange = onChange;
|
|
1375
|
+
this.onCapacity = onCapacity;
|
|
1368
1376
|
}
|
|
1369
1377
|
/**
|
|
1370
1378
|
* Says the numbers moved, and survives a listener that cannot take the news.
|
|
@@ -1384,6 +1392,34 @@ export class TaskQueue {
|
|
|
1384
1392
|
console.error("[queue] Error reporting the queue's state:", err);
|
|
1385
1393
|
}
|
|
1386
1394
|
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Says there may be room for another task, and survives a listener that
|
|
1397
|
+
* cannot take the news.
|
|
1398
|
+
*
|
|
1399
|
+
* Separate from `announce` because the two answer different questions. A
|
|
1400
|
+
* report of the numbers goes out whenever they move, including when a task
|
|
1401
|
+
* *starts* and the gateway has less room than before; this one fires only
|
|
1402
|
+
* where room may have appeared — a task finished, or the limit went up — and
|
|
1403
|
+
* it is what lets the gateway go and ask the workspace for more work.
|
|
1404
|
+
*
|
|
1405
|
+
* Guarded for the same reason `announce` is: filling the queue is a courtesy
|
|
1406
|
+
* to whoever is waiting, and a courtesy must not be able to break the task
|
|
1407
|
+
* that just finished.
|
|
1408
|
+
*/
|
|
1409
|
+
offerCapacity() {
|
|
1410
|
+
// Only when there is actually room. Both callers reach here on moves that
|
|
1411
|
+
// may leave none — a task finishing into a backlog that immediately
|
|
1412
|
+
// refills its slot, a limit *lowered* under work already running — and
|
|
1413
|
+
// "there may be room" is a message about the queue, not about the move.
|
|
1414
|
+
if (this.activeTasks >= this.maxConcurrency || this.queue.length > 0)
|
|
1415
|
+
return;
|
|
1416
|
+
try {
|
|
1417
|
+
this.onCapacity?.();
|
|
1418
|
+
}
|
|
1419
|
+
catch (err) {
|
|
1420
|
+
console.error("[queue] Error offering the queue's free capacity:", err);
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1387
1423
|
async run(taskFn) {
|
|
1388
1424
|
if (this.activeTasks < this.maxConcurrency) {
|
|
1389
1425
|
await this.execute(taskFn);
|
|
@@ -1421,6 +1457,9 @@ export class TaskQueue {
|
|
|
1421
1457
|
// state twice and never show the gap.
|
|
1422
1458
|
this.announce();
|
|
1423
1459
|
this.next();
|
|
1460
|
+
// After `next()`, so that whatever was already waiting here has taken its
|
|
1461
|
+
// slot back before anyone is told there is one going spare.
|
|
1462
|
+
this.offerCapacity();
|
|
1424
1463
|
}
|
|
1425
1464
|
}
|
|
1426
1465
|
/**
|
|
@@ -1468,6 +1507,11 @@ export class TaskQueue {
|
|
|
1468
1507
|
console.error(`[queue] Concurrency limit ${previous} -> ${maxConcurrency} ` +
|
|
1469
1508
|
`(${this.activeTasks} running, ${this.queue.length} queued)`);
|
|
1470
1509
|
this.next();
|
|
1510
|
+
// A raise with nothing waiting internally starts nothing, and used to end
|
|
1511
|
+
// there: the gateway sat at one task with room for three because the two
|
|
1512
|
+
// it could have run were still sitting in the workspace, unasked for.
|
|
1513
|
+
// (A lowered limit reaches here too, and leaves no room to offer.)
|
|
1514
|
+
this.offerCapacity();
|
|
1471
1515
|
}
|
|
1472
1516
|
}
|
|
1473
1517
|
/** Default maximum number of concurrent tasks allowed to prompt the ACP agent at once. */
|
|
@@ -1588,6 +1632,18 @@ export function parseGatewayArgs(args) {
|
|
|
1588
1632
|
i++;
|
|
1589
1633
|
}
|
|
1590
1634
|
break;
|
|
1635
|
+
case "--mcp-json":
|
|
1636
|
+
if (value) {
|
|
1637
|
+
options.mcpJsonPath = value;
|
|
1638
|
+
i++;
|
|
1639
|
+
}
|
|
1640
|
+
else {
|
|
1641
|
+
// Said out loud rather than ignored. The gateway would otherwise
|
|
1642
|
+
// start against whatever config happened to be near the working
|
|
1643
|
+
// directory, which is exactly what this flag was typed to avoid.
|
|
1644
|
+
console.error("[acp-gateway] Warning: --mcp-json provided without a path; ignoring.");
|
|
1645
|
+
}
|
|
1646
|
+
break;
|
|
1591
1647
|
case "--help":
|
|
1592
1648
|
case "-h":
|
|
1593
1649
|
options.command = "help";
|
|
@@ -1811,6 +1867,11 @@ BRIDGE
|
|
|
1811
1867
|
it before the turn is cancelled. Defaults to 30.
|
|
1812
1868
|
0 waits indefinitely, which is what a wedged
|
|
1813
1869
|
gateway looks like — use it knowingly.
|
|
1870
|
+
--mcp-json <path> An MCP config to read as well as the one found
|
|
1871
|
+
near the working directory, for running the
|
|
1872
|
+
gateway from outside its workspace. Any filename;
|
|
1873
|
+
a directory means the .mcp.json inside it. Its
|
|
1874
|
+
servers win where the names collide.
|
|
1814
1875
|
|
|
1815
1876
|
OTHER
|
|
1816
1877
|
--help, -h Show this help. Exits.
|
|
@@ -1824,9 +1885,11 @@ EXAMPLES
|
|
|
1824
1885
|
acp-gateway --agent-info --agent gemini See what that agent supports
|
|
1825
1886
|
acp-gateway --login -- gemini --acp Log in before running anything
|
|
1826
1887
|
acp-gateway --max-concurrency 4 -- gemini --acp
|
|
1888
|
+
acp-gateway --mcp-json ~/work/.mcp.json -- gemini --acp
|
|
1827
1889
|
|
|
1828
1890
|
The workspace comes from .mcp.json, searched for in the current directory and up
|
|
1829
|
-
to three directories above it
|
|
1891
|
+
to three directories above it. --mcp-json names one directly, wherever it lives
|
|
1892
|
+
and whatever it is called.`;
|
|
1830
1893
|
}
|
|
1831
1894
|
export function printHelp() {
|
|
1832
1895
|
console.log(helpText());
|
|
@@ -1859,7 +1922,7 @@ async function main() {
|
|
|
1859
1922
|
}
|
|
1860
1923
|
console.log(`Starting [acp-gateway] ${pkg.name} v${pkg.version}`);
|
|
1861
1924
|
// 1. Load MCP Config
|
|
1862
|
-
const configs = loadMcpConfig();
|
|
1925
|
+
const configs = loadMcpConfig(process.cwd(), options.mcpJsonPath);
|
|
1863
1926
|
const agentrqConfig = pickAgentrqServer(configs);
|
|
1864
1927
|
// 2. Work out what actually starts the agent — a registry id, or the command
|
|
1865
1928
|
// the user gave.
|
|
@@ -1888,8 +1951,18 @@ async function main() {
|
|
|
1888
1951
|
permissionConfig.timeoutMs = options.permissionTimeoutMs;
|
|
1889
1952
|
// 3. Initialize MCP Bridge
|
|
1890
1953
|
const mcpBridge = new MCPBridge(agentrqConfig);
|
|
1891
|
-
//
|
|
1892
|
-
|
|
1954
|
+
// What running a task fetched from the workspace takes, in the shape the
|
|
1955
|
+
// gateway's own path uses: a command to open a session from.
|
|
1956
|
+
const taskRunDeps = {
|
|
1957
|
+
acpCmdArgsOrConnection: acpCmdArgs,
|
|
1958
|
+
configsOrSessionSwitcher: configs,
|
|
1959
|
+
agentrqConfigOrAcpClient: agentrqConfig,
|
|
1960
|
+
};
|
|
1961
|
+
// After the bridge, because the queue now reports itself to it — and fills
|
|
1962
|
+
// itself from it: the answer to having room is to go and ask the workspace
|
|
1963
|
+
// for more work, or a limit of three runs one task and waits with the other
|
|
1964
|
+
// two still sitting in the workspace because nothing ever asked for them.
|
|
1965
|
+
const taskQueue = createSelfFillingTaskQueue(maxConcurrency, mcpBridge, taskRunDeps);
|
|
1893
1966
|
// Auth commands talk to the agent and exit; they never start bridging tasks.
|
|
1894
1967
|
// They run before the bridge connects, so a first-time login still works when
|
|
1895
1968
|
// the workspace is unreachable — `callTool` connects on demand if the login
|
|
@@ -2026,8 +2099,13 @@ async function main() {
|
|
|
2026
2099
|
if (idle !== "ready" && resolved.identity) {
|
|
2027
2100
|
void sendAgentIdentity(mcpBridge, resolved.identity);
|
|
2028
2101
|
}
|
|
2029
|
-
// Initial
|
|
2030
|
-
|
|
2102
|
+
// Initial fill: every slot the limit allows, not just the first. A gateway
|
|
2103
|
+
// that comes up to a workspace with work waiting should start as much of it
|
|
2104
|
+
// as it is allowed to run.
|
|
2105
|
+
//
|
|
2106
|
+
// Not awaited for the work itself — this resolves once the slots are filled,
|
|
2107
|
+
// and the tasks in them run on past it.
|
|
2108
|
+
await drainPendingTasks(mcpBridge, taskQueue, { ...taskRunDeps, taskQueue });
|
|
2031
2109
|
// Keep the process alive
|
|
2032
2110
|
await new Promise(() => { });
|
|
2033
2111
|
}
|
|
@@ -2041,11 +2119,15 @@ async function main() {
|
|
|
2041
2119
|
}
|
|
2042
2120
|
}
|
|
2043
2121
|
/**
|
|
2044
|
-
*
|
|
2045
|
-
*
|
|
2046
|
-
*
|
|
2122
|
+
* Takes the next not-started task off the workspace.
|
|
2123
|
+
*
|
|
2124
|
+
* `getTask` with no task id dequeues one task, so this is one task per call —
|
|
2125
|
+
* which is exactly why the gateway has to call it again to fill a second slot.
|
|
2126
|
+
*
|
|
2127
|
+
* Never throws: a workspace that cannot be reached is an outcome like any
|
|
2128
|
+
* other here, and the caller is in the middle of filling slots.
|
|
2047
2129
|
*/
|
|
2048
|
-
export async function
|
|
2130
|
+
export async function fetchNextTask(mcpBridge) {
|
|
2049
2131
|
console.error("[bridge] Checking for next task via MCP server...");
|
|
2050
2132
|
// Stamped before the fetch, so a cancel that arrives while `getTask` is in
|
|
2051
2133
|
// flight (or while the session is being opened) still stops the task.
|
|
@@ -2054,90 +2136,229 @@ export async function checkForNextTask(mcpBridge, acpCmdArgsOrConnection, config
|
|
|
2054
2136
|
const result = await mcpBridge.callTool("getTask");
|
|
2055
2137
|
if (result.isError) {
|
|
2056
2138
|
console.error("[mcp] Error getting next task:", result.content);
|
|
2057
|
-
return;
|
|
2139
|
+
return { status: "failed" };
|
|
2058
2140
|
}
|
|
2059
2141
|
const contentBlock = result.content;
|
|
2060
2142
|
const content = contentBlock[0];
|
|
2061
|
-
if (content
|
|
2062
|
-
content.text
|
|
2063
|
-
|
|
2064
|
-
const text = content.text;
|
|
2065
|
-
const taskId = extractTaskIdFromText(text);
|
|
2066
|
-
if (taskId) {
|
|
2067
|
-
if (lastTaskContent.get(taskId) === text) {
|
|
2068
|
-
console.log(`[bridge] Dropping repetitive checked task for ${taskId}`);
|
|
2069
|
-
return;
|
|
2070
|
-
}
|
|
2071
|
-
lastTaskContent.set(taskId, text);
|
|
2072
|
-
}
|
|
2073
|
-
console.error(`[bridge] Found task: "${text.slice(0, 50).replace(/\n/g, " ")}..."`);
|
|
2074
|
-
const runFn = async () => {
|
|
2075
|
-
if (isTaskCancelled(taskId, queuedSeq)) {
|
|
2076
|
-
console.error(`[bridge] Task ${taskId} was cancelled before execution started, skipping`);
|
|
2077
|
-
return;
|
|
2078
|
-
}
|
|
2079
|
-
let connectionToUse;
|
|
2080
|
-
let acpClientToUse;
|
|
2081
|
-
let sessionIdToUse;
|
|
2082
|
-
let actualAcpCmdArgs = [];
|
|
2083
|
-
let actualConfigs = [];
|
|
2084
|
-
let actualAgentrqConfig;
|
|
2085
|
-
if (Array.isArray(acpCmdArgsOrConnection)) {
|
|
2086
|
-
actualAcpCmdArgs = acpCmdArgsOrConnection;
|
|
2087
|
-
actualConfigs = configsOrSessionSwitcher;
|
|
2088
|
-
actualAgentrqConfig = agentrqConfigOrAcpClient;
|
|
2089
|
-
}
|
|
2090
|
-
else {
|
|
2091
|
-
connectionToUse = acpCmdArgsOrConnection;
|
|
2092
|
-
acpClientToUse = agentrqConfigOrAcpClient;
|
|
2093
|
-
const switcher = configsOrSessionSwitcher;
|
|
2094
|
-
if (switcher && typeof switcher.ensureForTask === "function") {
|
|
2095
|
-
sessionIdToUse = await switcher.ensureForTask(taskId);
|
|
2096
|
-
}
|
|
2097
|
-
actualAcpCmdArgs = acpCmdArgs || [];
|
|
2098
|
-
actualConfigs = configs || [];
|
|
2099
|
-
actualAgentrqConfig = agentrqConfig;
|
|
2100
|
-
}
|
|
2101
|
-
if (!connectionToUse) {
|
|
2102
|
-
const sessionInfo = await getOrCreateSession(taskId, actualAcpCmdArgs, actualConfigs, actualAgentrqConfig, mcpBridge);
|
|
2103
|
-
connectionToUse = sessionInfo.connection;
|
|
2104
|
-
sessionIdToUse = sessionInfo.sessionId;
|
|
2105
|
-
acpClientToUse = sessionInfo.acpClient;
|
|
2106
|
-
}
|
|
2107
|
-
// Spawning the agent and opening the session takes seconds; a cancel
|
|
2108
|
-
// that lands in that window has no turn to stop yet, so check again
|
|
2109
|
-
// before handing the agent the work.
|
|
2110
|
-
if (isTaskCancelled(taskId, queuedSeq)) {
|
|
2111
|
-
console.error(`[bridge] Task ${taskId} was cancelled during session setup, cancelling session`);
|
|
2112
|
-
await acpClientToUse?.cancelTurn(sessionIdToUse);
|
|
2113
|
-
return;
|
|
2114
|
-
}
|
|
2115
|
-
const promptResult = await connectionToUse.prompt({
|
|
2116
|
-
sessionId: sessionIdToUse,
|
|
2117
|
-
prompt: [{ type: "text", text }],
|
|
2118
|
-
});
|
|
2119
|
-
await acpClientToUse.flushReply(sessionIdToUse);
|
|
2120
|
-
await acpClientToUse.reportStopReason(sessionIdToUse, promptResult.stopReason);
|
|
2121
|
-
console.error(`\n[acp] Agent completed with: ${promptResult.stopReason}`);
|
|
2122
|
-
};
|
|
2123
|
-
// Serialised against any turn already running for this task, for the
|
|
2124
|
-
// reason runTurnForTask records: this path and the push notification can
|
|
2125
|
-
// both be handed the same task, and word it differently enough that the
|
|
2126
|
-
// repetition guard drops neither.
|
|
2127
|
-
//
|
|
2128
|
-
// Outside the queue slot rather than inside it, so a delivery that is
|
|
2129
|
-
// only waiting for the turn ahead of it is not also holding a share of
|
|
2130
|
-
// the concurrency limit while it waits.
|
|
2131
|
-
await runTurnForTask(taskId, () => taskQueue ? taskQueue.run(runFn) : runFn());
|
|
2132
|
-
}
|
|
2133
|
-
else {
|
|
2143
|
+
if (!content ||
|
|
2144
|
+
!content.text ||
|
|
2145
|
+
content.text.includes("no pending tasks exist")) {
|
|
2134
2146
|
console.error("[bridge] No pending tasks available.");
|
|
2147
|
+
return { status: "empty" };
|
|
2148
|
+
}
|
|
2149
|
+
const text = content.text;
|
|
2150
|
+
const taskId = extractTaskIdFromText(text);
|
|
2151
|
+
if (taskId) {
|
|
2152
|
+
if (lastTaskContent.get(taskId) === text) {
|
|
2153
|
+
console.log(`[bridge] Dropping repetitive checked task for ${taskId}`);
|
|
2154
|
+
return { status: "skipped" };
|
|
2155
|
+
}
|
|
2156
|
+
lastTaskContent.set(taskId, text);
|
|
2135
2157
|
}
|
|
2158
|
+
console.error(`[bridge] Found task: "${text.slice(0, 50).replace(/\n/g, " ")}..."`);
|
|
2159
|
+
return { status: "task", task: { text, taskId, queuedSeq } };
|
|
2136
2160
|
}
|
|
2137
2161
|
catch (err) {
|
|
2138
2162
|
console.error("[bridge] Failed to check for next task:", err);
|
|
2163
|
+
return { status: "failed" };
|
|
2139
2164
|
}
|
|
2140
2165
|
}
|
|
2166
|
+
/**
|
|
2167
|
+
* Hands a fetched task to the agent, through the queue and behind any turn the
|
|
2168
|
+
* task already has running.
|
|
2169
|
+
*
|
|
2170
|
+
* Resolves when the agent is done with it, so a caller that wants to fill the
|
|
2171
|
+
* *next* slot must not wait for this one.
|
|
2172
|
+
*/
|
|
2173
|
+
export async function startPendingTask(task, mcpBridge, deps) {
|
|
2174
|
+
const { text, taskId, queuedSeq } = task;
|
|
2175
|
+
const runFn = async () => {
|
|
2176
|
+
if (isTaskCancelled(taskId, queuedSeq)) {
|
|
2177
|
+
console.error(`[bridge] Task ${taskId} was cancelled before execution started, skipping`);
|
|
2178
|
+
return;
|
|
2179
|
+
}
|
|
2180
|
+
let connectionToUse;
|
|
2181
|
+
let acpClientToUse;
|
|
2182
|
+
let sessionIdToUse;
|
|
2183
|
+
let actualAcpCmdArgs = [];
|
|
2184
|
+
let actualConfigs = [];
|
|
2185
|
+
let actualAgentrqConfig;
|
|
2186
|
+
if (Array.isArray(deps.acpCmdArgsOrConnection)) {
|
|
2187
|
+
actualAcpCmdArgs = deps.acpCmdArgsOrConnection;
|
|
2188
|
+
actualConfigs = deps.configsOrSessionSwitcher;
|
|
2189
|
+
actualAgentrqConfig = deps.agentrqConfigOrAcpClient;
|
|
2190
|
+
}
|
|
2191
|
+
else {
|
|
2192
|
+
connectionToUse = deps.acpCmdArgsOrConnection;
|
|
2193
|
+
acpClientToUse = deps.agentrqConfigOrAcpClient;
|
|
2194
|
+
const switcher = deps.configsOrSessionSwitcher;
|
|
2195
|
+
if (switcher && typeof switcher.ensureForTask === "function") {
|
|
2196
|
+
sessionIdToUse = await switcher.ensureForTask(taskId);
|
|
2197
|
+
}
|
|
2198
|
+
actualAcpCmdArgs = deps.acpCmdArgs || [];
|
|
2199
|
+
actualConfigs = deps.configs || [];
|
|
2200
|
+
actualAgentrqConfig = deps.agentrqConfig;
|
|
2201
|
+
}
|
|
2202
|
+
if (!connectionToUse) {
|
|
2203
|
+
const sessionInfo = await getOrCreateSession(taskId, actualAcpCmdArgs, actualConfigs, actualAgentrqConfig, mcpBridge);
|
|
2204
|
+
connectionToUse = sessionInfo.connection;
|
|
2205
|
+
sessionIdToUse = sessionInfo.sessionId;
|
|
2206
|
+
acpClientToUse = sessionInfo.acpClient;
|
|
2207
|
+
}
|
|
2208
|
+
// Spawning the agent and opening the session takes seconds; a cancel
|
|
2209
|
+
// that lands in that window has no turn to stop yet, so check again
|
|
2210
|
+
// before handing the agent the work.
|
|
2211
|
+
if (isTaskCancelled(taskId, queuedSeq)) {
|
|
2212
|
+
console.error(`[bridge] Task ${taskId} was cancelled during session setup, cancelling session`);
|
|
2213
|
+
await acpClientToUse?.cancelTurn(sessionIdToUse);
|
|
2214
|
+
return;
|
|
2215
|
+
}
|
|
2216
|
+
const promptResult = await connectionToUse.prompt({
|
|
2217
|
+
sessionId: sessionIdToUse,
|
|
2218
|
+
prompt: [{ type: "text", text }],
|
|
2219
|
+
});
|
|
2220
|
+
await acpClientToUse.flushReply(sessionIdToUse);
|
|
2221
|
+
await acpClientToUse.reportStopReason(sessionIdToUse, promptResult.stopReason);
|
|
2222
|
+
console.error(`\n[acp] Agent completed with: ${promptResult.stopReason}`);
|
|
2223
|
+
};
|
|
2224
|
+
// Serialised against any turn already running for this task, for the
|
|
2225
|
+
// reason runTurnForTask records: this path and the push notification can
|
|
2226
|
+
// both be handed the same task, and word it differently enough that the
|
|
2227
|
+
// repetition guard drops neither.
|
|
2228
|
+
//
|
|
2229
|
+
// Outside the queue slot rather than inside it, so a delivery that is
|
|
2230
|
+
// only waiting for the turn ahead of it is not also holding a share of
|
|
2231
|
+
// the concurrency limit while it waits.
|
|
2232
|
+
await runTurnForTask(taskId, () => deps.taskQueue ? deps.taskQueue.run(runFn) : runFn());
|
|
2233
|
+
}
|
|
2234
|
+
/** How many more tasks the queue could start right now. */
|
|
2235
|
+
export function freeSlots(queue) {
|
|
2236
|
+
return Math.max(0, queue.getMaxConcurrency() - queue.getActiveCount() - queue.getQueueLength());
|
|
2237
|
+
}
|
|
2238
|
+
/**
|
|
2239
|
+
* The drain in progress, if one is, and whether room appeared while it ran.
|
|
2240
|
+
*
|
|
2241
|
+
* One drain at a time, for the same reason there is one concurrency report at
|
|
2242
|
+
* a time: every trigger for this — a task finishing, a limit being raised —
|
|
2243
|
+
* can land while a drain is already waiting on `getTask`, and a second drain
|
|
2244
|
+
* started there would be asking the workspace for the task the first one is
|
|
2245
|
+
* about to be handed.
|
|
2246
|
+
*
|
|
2247
|
+
* The flag is what keeps collapsing them from losing one. A slot that came
|
|
2248
|
+
* free during a fetch is real, and the drain that swallowed the news may
|
|
2249
|
+
* already have decided the workspace was empty — so the news is answered once
|
|
2250
|
+
* that drain finishes rather than dropped.
|
|
2251
|
+
*/
|
|
2252
|
+
let drainOnTheWire = null;
|
|
2253
|
+
let capacityMovedAgain = false;
|
|
2254
|
+
/** Forgets any drain in flight. For tests. */
|
|
2255
|
+
export function resetPendingTaskDrain() {
|
|
2256
|
+
drainOnTheWire = null;
|
|
2257
|
+
capacityMovedAgain = false;
|
|
2258
|
+
}
|
|
2259
|
+
/**
|
|
2260
|
+
* How many repeats in a row the drain will take before it stops asking.
|
|
2261
|
+
*
|
|
2262
|
+
* A repeat has been dequeued and dropped, so the slot it should have filled is
|
|
2263
|
+
* still free and the task behind it may well fill it — but a workspace handing
|
|
2264
|
+
* back the same task forever must not hold the gateway in a fetch loop.
|
|
2265
|
+
*/
|
|
2266
|
+
export const MAX_SKIPPED_FETCHES = 3;
|
|
2267
|
+
/**
|
|
2268
|
+
* Fills every free slot the queue has with work from the workspace.
|
|
2269
|
+
*
|
|
2270
|
+
* The gateway used to ask for exactly one task, once, at startup — which was
|
|
2271
|
+
* the whole story while it could only run one. Above that, three tasks waiting
|
|
2272
|
+
* in a workspace and three slots here meant one task running and two slots
|
|
2273
|
+
* idle, because nothing ever went back to ask for the second and third.
|
|
2274
|
+
*
|
|
2275
|
+
* Fetches are sequential and the runs are not: `getTask` dequeues a task, so
|
|
2276
|
+
* two fetches in flight at once are two chances to be handed the same one,
|
|
2277
|
+
* while waiting for a task to *finish* before fetching the next would defeat
|
|
2278
|
+
* the point entirely. So each task is started and left to run, and the next
|
|
2279
|
+
* fetch goes out immediately behind it.
|
|
2280
|
+
*
|
|
2281
|
+
* Stops as soon as the workspace says it has nothing — the run that frees the
|
|
2282
|
+
* next slot brings us back here anyway.
|
|
2283
|
+
*/
|
|
2284
|
+
export function drainPendingTasks(mcpBridge, queue, deps) {
|
|
2285
|
+
if (drainOnTheWire) {
|
|
2286
|
+
capacityMovedAgain = true;
|
|
2287
|
+
return drainOnTheWire;
|
|
2288
|
+
}
|
|
2289
|
+
// The slot is cleared from a `.finally` on the promise rather than from
|
|
2290
|
+
// inside `pullUntilFull`. An async function that never reaches an `await` —
|
|
2291
|
+
// which is exactly what a drain with no free slot is — runs to completion
|
|
2292
|
+
// before its promise is ever handed back, so a clear written inside it would
|
|
2293
|
+
// happen *before* the assignment below and leave a settled promise sitting
|
|
2294
|
+
// in the slot for the rest of the process. Every later drain would then find
|
|
2295
|
+
// the slot occupied and quietly fetch nothing, which is this whole feature
|
|
2296
|
+
// switching itself off. `.finally` cannot beat the assignment: it settles a
|
|
2297
|
+
// microtask later at the earliest.
|
|
2298
|
+
const drain = pullUntilFull(mcpBridge, queue, deps).finally(() => {
|
|
2299
|
+
drainOnTheWire = null;
|
|
2300
|
+
});
|
|
2301
|
+
drainOnTheWire = drain;
|
|
2302
|
+
return drain;
|
|
2303
|
+
}
|
|
2304
|
+
/** Fetches and starts tasks until the queue is full or the workspace is empty. */
|
|
2305
|
+
async function pullUntilFull(mcpBridge, queue, deps) {
|
|
2306
|
+
do {
|
|
2307
|
+
// Read at the top of each pass: room that appears while this one is
|
|
2308
|
+
// mid-fetch is answered by the next pass rather than lost, and joining a
|
|
2309
|
+
// drain already running is what set it.
|
|
2310
|
+
capacityMovedAgain = false;
|
|
2311
|
+
// Tasks started but waiting on an earlier turn for their own chat. They
|
|
2312
|
+
// have taken no slot and will not until that turn ends, so the queue still
|
|
2313
|
+
// reads as having room — and without counting them the loop fetches
|
|
2314
|
+
// against that same room over and over, emptying the workspace into this
|
|
2315
|
+
// process where nothing else can hand the work to anyone else.
|
|
2316
|
+
let parked = 0;
|
|
2317
|
+
let skipped = 0;
|
|
2318
|
+
while (freeSlots(queue) - parked > 0) {
|
|
2319
|
+
const outcome = await fetchNextTask(mcpBridge);
|
|
2320
|
+
if (outcome.status === "empty" || outcome.status === "failed")
|
|
2321
|
+
break;
|
|
2322
|
+
if (outcome.status === "skipped") {
|
|
2323
|
+
// Dropped as a repeat of something already running: the slot is still
|
|
2324
|
+
// free, so try what is behind it rather than abandoning the slot.
|
|
2325
|
+
if (++skipped >= MAX_SKIPPED_FETCHES)
|
|
2326
|
+
break;
|
|
2327
|
+
continue;
|
|
2328
|
+
}
|
|
2329
|
+
skipped = 0;
|
|
2330
|
+
const { task } = outcome;
|
|
2331
|
+
const heldBefore = queue.getActiveCount() + queue.getQueueLength();
|
|
2332
|
+
// Deliberately not awaited: this task is running now, and the slot
|
|
2333
|
+
// beside it is what this loop is here to fill.
|
|
2334
|
+
void startPendingTask(task, mcpBridge, { ...deps, taskQueue: queue }).catch((err) => {
|
|
2335
|
+
console.error(`[bridge] Error running task ${task.taskId ?? "(unnamed)"}:`, err);
|
|
2336
|
+
});
|
|
2337
|
+
// `run` takes its slot before it yields, so a queue that did not move is
|
|
2338
|
+
// a task parked behind a turn rather than one holding a slot.
|
|
2339
|
+
if (queue.getActiveCount() + queue.getQueueLength() === heldBefore)
|
|
2340
|
+
parked++;
|
|
2341
|
+
}
|
|
2342
|
+
} while (capacityMovedAgain);
|
|
2343
|
+
}
|
|
2344
|
+
/**
|
|
2345
|
+
* Builds a queue that goes and fetches its own work.
|
|
2346
|
+
*
|
|
2347
|
+
* Out here rather than inline in `main()` for the reason `createTaskQueue`
|
|
2348
|
+
* records: `main()` spawns processes and never returns, so wiring written
|
|
2349
|
+
* inside it is wiring nothing exercises. And this is wiring worth exercising —
|
|
2350
|
+
* it is the whole of "a gateway with room for three tasks runs three tasks".
|
|
2351
|
+
*
|
|
2352
|
+
* The callback names the queue it is being handed to, which is safe for the
|
|
2353
|
+
* same reason `createTaskQueue`'s is: it only ever runs later, from inside the
|
|
2354
|
+
* queue's own methods.
|
|
2355
|
+
*/
|
|
2356
|
+
export function createSelfFillingTaskQueue(maxConcurrency, mcpBridge, deps) {
|
|
2357
|
+
const queue = createTaskQueue(maxConcurrency, mcpBridge, () => {
|
|
2358
|
+
void drainPendingTasks(mcpBridge, queue, { ...deps, taskQueue: queue });
|
|
2359
|
+
});
|
|
2360
|
+
return queue;
|
|
2361
|
+
}
|
|
2141
2362
|
if (process.env.NODE_ENV !== "test") {
|
|
2142
2363
|
// Keep the gateway alive across transient failures. The MCP transport has its
|
|
2143
2364
|
// own reconnect logic (mcpClient.ts), so a stray rejected promise or async
|