send-to-paseo 2.4.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.server.ts +3 -1
- package/package.json +2 -2
- package/server/daemon.ts +74 -47
- package/server/shared-daemon.ts +117 -0
- package/shared/contracts.ts +1 -1
package/index.server.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
updateConfig,
|
|
18
18
|
} from "./shared/contracts";
|
|
19
19
|
import { getBridgeStatus, restartBridge, runBridge } from "./server/bridge";
|
|
20
|
+
import { closeDaemon } from "./server/daemon";
|
|
20
21
|
import { dependencySnapshot } from "./server/deps";
|
|
21
22
|
import {
|
|
22
23
|
listAgentProfiles,
|
|
@@ -178,5 +179,6 @@ export default function contribute(server: PluginServerContext) {
|
|
|
178
179
|
});
|
|
179
180
|
|
|
180
181
|
const stopBridge = runBridge();
|
|
181
|
-
|
|
182
|
+
// The held daemon connection closes after the bridge has stopped serving requests.
|
|
183
|
+
return async () => { removeTools(); await stopBridge(); await closeDaemon(); };
|
|
182
184
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "send-to-paseo",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.4.
|
|
4
|
+
"version": "2.4.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Local HTTP bridge that dispatches PR work to the right Paseo workspace and agent.",
|
|
7
7
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"typecheck": "tsc --noEmit",
|
|
38
|
-
"test": "node check-deps.mjs && node --test client/hosts.test.mjs server/main-agent.test.mjs server/follow-up.test.mjs",
|
|
38
|
+
"test": "node check-deps.mjs && node --test client/hosts.test.mjs server/main-agent.test.mjs server/follow-up.test.mjs server/daemon-connection.test.mjs",
|
|
39
39
|
"verify": "npm run typecheck && npm test && node --test --experimental-strip-types agent-tools.test.mjs shared/chat.test.mjs && node --test native-tools.test.mjs"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
package/server/daemon.ts
CHANGED
|
@@ -7,20 +7,27 @@ import { BridgeError } from "../shared/contracts";
|
|
|
7
7
|
import { resolveDaemonPassword } from "./daemon-password";
|
|
8
8
|
import { settings } from "./settings";
|
|
9
9
|
import { invokeFollowUp } from "./follow-up-id";
|
|
10
|
+
import { sharedDaemon } from "./shared-daemon";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
+
* The plugin's one held daemon connection (one-held-daemon-connection, AGENTS.md).
|
|
13
14
|
*
|
|
14
15
|
* Only RPC handlers receive a `paseo` handle, and the bridge serves requests
|
|
15
|
-
* that arrive long before any surface is opened, so it
|
|
16
|
-
* connection
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* that arrive long before any surface is opened, so it keeps its own
|
|
17
|
+
* connection. Every bridge request (`withPaseo`) and every optional Follow Up
|
|
18
|
+
* call (`callFollowUp`) runs on the same held DaemonClient: a request gets a
|
|
19
|
+
* scoped SDK view (`createPaseoApi`) that is disposed when it ends, never a new
|
|
20
|
+
* socket. A drop retires the connection and the next request opens one fresh;
|
|
21
|
+
* `closeDaemon` in the plugin's teardown closes it, so a held socket never
|
|
22
|
+
* hangs Paseo's "Stopping plugin" step. 2026-11-17 live: a connection per
|
|
23
|
+
* request meant about 20 hellos a minute for each of the two client names
|
|
24
|
+
* while a browser had the extension open.
|
|
20
25
|
*/
|
|
21
26
|
|
|
22
27
|
const CONNECT_TIMEOUT_MS = 10_000;
|
|
23
28
|
const STATUS_TIMEOUT_MS = 2_000;
|
|
29
|
+
/** An optional Follow Up call, connect included, gives up after this long. */
|
|
30
|
+
const FOLLOW_UP_TIMEOUT_MS = 2_000;
|
|
24
31
|
const STATUS_TTL_MS = 15_000;
|
|
25
32
|
|
|
26
33
|
function timeoutSignal(ms: number): AbortSignal {
|
|
@@ -33,45 +40,54 @@ function timeoutSignal(ms: number): AbortSignal {
|
|
|
33
40
|
|
|
34
41
|
type PaseoApi = PluginHandlerContext["paseo"];
|
|
35
42
|
|
|
43
|
+
/** The subset of Paseo's DaemonClient this plugin uses. */
|
|
44
|
+
interface DaemonClient {
|
|
45
|
+
connect(): Promise<void>;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
invokePluginRpc(plugin: string, method: string, input: unknown): Promise<unknown>;
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
interface ClientModule {
|
|
37
|
-
|
|
51
|
+
createPaseoApi(client: DaemonClient): PaseoApi & { dispose(): Promise<void> };
|
|
52
|
+
DaemonClient: new (options: {
|
|
38
53
|
url: string;
|
|
39
54
|
clientId: string;
|
|
55
|
+
clientType: "cli";
|
|
40
56
|
reconnect: { enabled: boolean };
|
|
41
57
|
connectTimeoutMs: number;
|
|
42
58
|
suppressSendErrors: boolean;
|
|
43
59
|
password?: string;
|
|
44
|
-
})
|
|
45
|
-
connect(): Promise<void>;
|
|
46
|
-
close(): Promise<void>;
|
|
47
|
-
};
|
|
60
|
+
}) => DaemonClient;
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
let clientModule: ClientModule | null = null;
|
|
51
64
|
|
|
52
65
|
/**
|
|
53
|
-
* Borrows Paseo's own SDK from the host at runtime.
|
|
66
|
+
* Borrows Paseo's own SDK and daemon client from the host at runtime.
|
|
54
67
|
*
|
|
55
|
-
* The
|
|
56
|
-
* compiler cannot resolve
|
|
68
|
+
* The specifiers are assembled rather than written as literals so the plugin
|
|
69
|
+
* compiler cannot resolve them at build time. That keeps the plugin installable
|
|
57
70
|
* from a directory or from Git without a package manager step, and it removes
|
|
58
71
|
* any chance of a protocol version skew between a bundled copy of the SDK and
|
|
59
72
|
* the daemon actually running.
|
|
60
73
|
*/
|
|
61
74
|
function loadClientModule(): ClientModule {
|
|
62
75
|
if (clientModule !== null) return clientModule;
|
|
63
|
-
const
|
|
76
|
+
const sdk = ["@getpaseo", "client"].join("/");
|
|
77
|
+
const internal = [sdk, "internal", "daemon-client"].join("/");
|
|
64
78
|
// Paseo compiles plugin backends to CommonJS, so `require` is normally right
|
|
65
79
|
// here. `createRequire` keeps this working if a host ever loads the module as
|
|
66
80
|
// real ESM instead, where `require` is not defined.
|
|
67
81
|
const load: NodeRequire =
|
|
68
82
|
typeof require === "function" ? require : createRequire(join(process.cwd(), "noop.cjs"));
|
|
69
83
|
try {
|
|
70
|
-
|
|
84
|
+
const { createPaseoApi } = load(sdk) as Pick<ClientModule, "createPaseoApi">;
|
|
85
|
+
const { DaemonClient } = load(internal) as Pick<ClientModule, "DaemonClient">;
|
|
86
|
+
clientModule = { createPaseoApi, DaemonClient };
|
|
71
87
|
} catch (error) {
|
|
72
88
|
throw new BridgeError(
|
|
73
89
|
"daemon_unreachable",
|
|
74
|
-
`This Paseo host does not expose its client SDK (${
|
|
90
|
+
`This Paseo host does not expose its client SDK (${sdk}), which the Send to Paseo bridge needs: ${
|
|
75
91
|
error instanceof Error ? error.message : String(error)
|
|
76
92
|
}`,
|
|
77
93
|
);
|
|
@@ -137,33 +153,17 @@ async function resolvePassword(): Promise<string | undefined> {
|
|
|
137
153
|
return resolveDaemonPassword(async () => (await settings.read()).daemonPassword);
|
|
138
154
|
}
|
|
139
155
|
|
|
140
|
-
/**
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
};
|
|
147
|
-
};
|
|
148
|
-
const password = await resolvePassword();
|
|
149
|
-
const client = new DaemonClient({ url: await resolveUrl(), clientId: "send-to-paseo-follow-up", clientType: "cli", reconnect: { enabled: false }, connectTimeoutMs: 2_000, suppressSendErrors: true, ...(password ? { password } : {}) });
|
|
150
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
151
|
-
try {
|
|
152
|
-
await client.connect();
|
|
153
|
-
return await Promise.race([
|
|
154
|
-
invokeFollowUp((plugin, m, i) => client.invokePluginRpc(plugin, m, i), method, input),
|
|
155
|
-
new Promise<never>((_, reject) => { timer = setTimeout(() => reject(new Error("Optional Follow Up request timed out.")), 2_000); timer.unref?.(); }),
|
|
156
|
-
]);
|
|
157
|
-
} finally { if (timer) clearTimeout(timer); await client.close().catch(() => undefined); }
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** Runs `work` against an SDK connection that is always closed before returning. */
|
|
161
|
-
export async function withPaseo<T>(work: (paseo: PaseoApi) => Promise<T>): Promise<T> {
|
|
162
|
-
const { createPaseoClient } = loadClientModule();
|
|
156
|
+
/**
|
|
157
|
+
* Builds and connects the one client. The only place a DaemonClient is built;
|
|
158
|
+
* a failure here is a `daemon_unreachable` BridgeError.
|
|
159
|
+
*/
|
|
160
|
+
async function openDaemon(): Promise<DaemonClient> {
|
|
161
|
+
const { DaemonClient } = loadClientModule();
|
|
163
162
|
const password = await resolvePassword();
|
|
164
|
-
const client =
|
|
163
|
+
const client = new DaemonClient({
|
|
165
164
|
url: await resolveUrl(),
|
|
166
|
-
clientId: "send-to-paseo
|
|
165
|
+
clientId: "send-to-paseo",
|
|
166
|
+
clientType: "cli",
|
|
167
167
|
reconnect: { enabled: false },
|
|
168
168
|
connectTimeoutMs: CONNECT_TIMEOUT_MS,
|
|
169
169
|
suppressSendErrors: true,
|
|
@@ -173,6 +173,7 @@ export async function withPaseo<T>(work: (paseo: PaseoApi) => Promise<T>): Promi
|
|
|
173
173
|
});
|
|
174
174
|
try {
|
|
175
175
|
await client.connect();
|
|
176
|
+
return client;
|
|
176
177
|
} catch (error) {
|
|
177
178
|
await client.close().catch(() => undefined);
|
|
178
179
|
const detail = error instanceof Error ? error.message : String(error);
|
|
@@ -191,12 +192,38 @@ export async function withPaseo<T>(work: (paseo: PaseoApi) => Promise<T>): Promi
|
|
|
191
192
|
detail,
|
|
192
193
|
);
|
|
193
194
|
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const shared = sharedDaemon(openDaemon);
|
|
198
|
+
|
|
199
|
+
/** Closes the held connection. The plugin's teardown runs it after the bridge stops. */
|
|
200
|
+
export function closeDaemon(): Promise<void> {
|
|
201
|
+
return shared.close();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Optional companion RPCs always target this daemon, never a browser-selected host. */
|
|
205
|
+
export async function callFollowUp(method: "follow.pr-owner" | "follow.pr-workspaces" | "follow.browser-exchange", input: unknown): Promise<unknown> {
|
|
206
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
194
207
|
try {
|
|
195
|
-
return await
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
208
|
+
return await Promise.race([
|
|
209
|
+
shared.run((client) => invokeFollowUp((plugin, m, i) => client.invokePluginRpc(plugin, m, i), method, input)),
|
|
210
|
+
new Promise<never>((_, reject) => { timer = setTimeout(() => reject(new Error("Optional Follow Up request timed out.")), FOLLOW_UP_TIMEOUT_MS); timer.unref?.(); }),
|
|
211
|
+
]);
|
|
212
|
+
} finally { if (timer) clearTimeout(timer); }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Runs `work` against a scoped SDK view of the held connection, released before returning. */
|
|
216
|
+
export async function withPaseo<T>(work: (paseo: PaseoApi) => Promise<T>): Promise<T> {
|
|
217
|
+
const { createPaseoApi } = loadClientModule();
|
|
218
|
+
return shared.run(async (client) => {
|
|
219
|
+
const paseo = createPaseoApi(client);
|
|
220
|
+
try {
|
|
221
|
+
return await work(paseo);
|
|
222
|
+
} finally {
|
|
223
|
+
// Releases this request's handles and listeners, never the socket.
|
|
224
|
+
await paseo.dispose().catch(() => undefined);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
200
227
|
}
|
|
201
228
|
|
|
202
229
|
export interface DaemonStatus {
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One held daemon connection, shared by every caller in this process
|
|
3
|
+
* (one-held-daemon-connection, AGENTS.md).
|
|
4
|
+
*
|
|
5
|
+
* Concurrent callers join the open in flight. A real drop (the client's own
|
|
6
|
+
* connection status, or a call that failed on a socket no longer `connected`)
|
|
7
|
+
* retires it, and the next call opens one fresh connection. A failed open backs
|
|
8
|
+
* off, and calls inside that window fail fast with the same error. A connection
|
|
9
|
+
* older than `maxMs` is replaced once its in-flight calls finish. `close()` is
|
|
10
|
+
* the plugin's teardown: nothing opens afterwards, so the subprocess can exit.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface Closable {
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ConnectionStatus = { status: string };
|
|
18
|
+
type StatusSource = {
|
|
19
|
+
getConnectionState?(): ConnectionStatus;
|
|
20
|
+
subscribeConnectionStatus?(listener: (state: ConnectionStatus) => void): () => void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface SharedDaemon<C extends Closable> {
|
|
24
|
+
run<T>(work: (client: C) => Promise<T>): Promise<T>;
|
|
25
|
+
close(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const SHARED_CLIENT_MAX_MS = 30 * 60_000;
|
|
29
|
+
export const OPEN_BACKOFF_MS = 1_000;
|
|
30
|
+
export const OPEN_BACKOFF_MAX_MS = 30_000;
|
|
31
|
+
|
|
32
|
+
export function sharedDaemon<C extends Closable>(
|
|
33
|
+
open: () => Promise<C>,
|
|
34
|
+
options: { maxMs?: number; now?: () => number; backoffMs?: number; backoffMaxMs?: number; stopping?: () => Error } = {},
|
|
35
|
+
): SharedDaemon<C> {
|
|
36
|
+
const maxMs = options.maxMs ?? SHARED_CLIENT_MAX_MS;
|
|
37
|
+
const now = options.now ?? Date.now;
|
|
38
|
+
const backoffMs = options.backoffMs ?? OPEN_BACKOFF_MS;
|
|
39
|
+
const backoffMaxMs = options.backoffMaxMs ?? OPEN_BACKOFF_MAX_MS;
|
|
40
|
+
const stopping = options.stopping ?? (() => new Error("Send to Paseo is stopping."));
|
|
41
|
+
type Entry = { client: Promise<C>; opened: number; users: number; retired: boolean; unsubscribe?: () => void };
|
|
42
|
+
let current: Entry | null = null;
|
|
43
|
+
let stopped = false;
|
|
44
|
+
let failures = 0;
|
|
45
|
+
let retryAt = 0;
|
|
46
|
+
let lastError: unknown = null;
|
|
47
|
+
const all = new Set<Entry>();
|
|
48
|
+
|
|
49
|
+
const shut = async (entry: Entry) => {
|
|
50
|
+
all.delete(entry);
|
|
51
|
+
if (current === entry) current = null;
|
|
52
|
+
entry.unsubscribe?.();
|
|
53
|
+
const client = await entry.client.catch(() => null);
|
|
54
|
+
if (client) await client.close().catch(() => undefined);
|
|
55
|
+
};
|
|
56
|
+
const retire = (entry: Entry) => {
|
|
57
|
+
entry.retired = true;
|
|
58
|
+
if (current === entry) current = null;
|
|
59
|
+
if (entry.users === 0) void shut(entry);
|
|
60
|
+
};
|
|
61
|
+
const live = (client: C) => {
|
|
62
|
+
const status = (client as unknown as StatusSource).getConnectionState?.().status;
|
|
63
|
+
return status === undefined || status === "connected";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const run = async <T>(work: (client: C) => Promise<T>): Promise<T> => {
|
|
67
|
+
if (stopped) throw stopping();
|
|
68
|
+
if (current && now() - current.opened >= maxMs) retire(current);
|
|
69
|
+
if (!current) {
|
|
70
|
+
if (failures > 0 && now() < retryAt) throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
71
|
+
const entry = { opened: now(), users: 0, retired: false } as Entry;
|
|
72
|
+
entry.client = open().then(
|
|
73
|
+
(client) => {
|
|
74
|
+
failures = 0;
|
|
75
|
+
if (stopped) {
|
|
76
|
+
void client.close().catch(() => undefined);
|
|
77
|
+
throw stopping();
|
|
78
|
+
}
|
|
79
|
+
entry.unsubscribe = (client as unknown as StatusSource).subscribeConnectionStatus?.((state) => {
|
|
80
|
+
if (state.status === "disconnected" || state.status === "disposed") retire(entry);
|
|
81
|
+
});
|
|
82
|
+
return client;
|
|
83
|
+
},
|
|
84
|
+
(error: unknown) => {
|
|
85
|
+
failures++;
|
|
86
|
+
lastError = error;
|
|
87
|
+
retryAt = now() + Math.min(backoffMs * 2 ** (failures - 1), backoffMaxMs);
|
|
88
|
+
all.delete(entry);
|
|
89
|
+
if (current === entry) current = null;
|
|
90
|
+
throw error;
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
current = entry;
|
|
94
|
+
all.add(entry);
|
|
95
|
+
}
|
|
96
|
+
const entry = current;
|
|
97
|
+
entry.users++;
|
|
98
|
+
try {
|
|
99
|
+
const client = await entry.client;
|
|
100
|
+
try {
|
|
101
|
+
return await work(client);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (!live(client)) retire(entry);
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
} finally {
|
|
107
|
+
entry.users--;
|
|
108
|
+
if (entry.retired && entry.users === 0 && all.has(entry)) void shut(entry);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const close = async () => {
|
|
113
|
+
stopped = true;
|
|
114
|
+
await Promise.allSettled([...all].map((entry) => shut(entry)));
|
|
115
|
+
};
|
|
116
|
+
return { run, close };
|
|
117
|
+
}
|
package/shared/contracts.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { z } from "zod";
|
|
|
9
9
|
|
|
10
10
|
/** Advertised in `GET /v1/ping`. Keep in step with `package.json`. */
|
|
11
11
|
export const PLUGIN_NAME = "send-to-paseo";
|
|
12
|
-
export const PLUGIN_VERSION = "2.4.
|
|
12
|
+
export const PLUGIN_VERSION = "2.4.2";
|
|
13
13
|
/** Bumped only for an incompatible bridge API; the paths stay `/v1`. */
|
|
14
14
|
export const CONTRACT_VERSION = 1;
|
|
15
15
|
|