agent-yes 1.299.1 → 1.300.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/README.md +9 -0
- package/default.config.yaml +12 -0
- package/dist/{SUPPORTED_CLIS-MftynU62.js → SUPPORTED_CLIS-BAeTuI-p.js} +3 -3
- package/dist/{SUPPORTED_CLIS-YZvIGQvw.js → SUPPORTED_CLIS-BZlaxyEH.js} +2 -2
- package/dist/{agentShare-dKQQSOD7.js → agentShare-DGQM24wU.js} +3 -3
- package/dist/{callback-BEbbWLKe.js → callback-3RsdcuV0.js} +3 -3
- package/dist/{callback-DWxTSlly.js → callback-CQadU_zP.js} +3 -3
- package/dist/cli.js +5 -5
- package/dist/{cmdPs-D7HWttUU.js → cmdPs-CQxFAs9M.js} +3 -3
- package/dist/index.js +2 -2
- package/dist/{notifyDaemon-B0aU7koS.js → notifyDaemon-CxkiJvOo.js} +3 -3
- package/dist/{parentPingLoop-DSiOXQeU.js → parentPingLoop-vZ8I9k5K.js} +2 -2
- package/dist/{remotes-DrYe_nfw.js → remotes-B4v0Nowo.js} +64 -7
- package/dist/remotes-DnMQWaqQ.js +5 -0
- package/dist/{rustBinary-CKuufNRn.js → rustBinary-B1buVId2.js} +2 -2
- package/dist/{rustBinary-DgRg5H6V.js → rustBinary-etb0Jsga.js} +2 -2
- package/dist/{schedule-BStFx0_s.js → schedule-CMBHLSlz.js} +4 -4
- package/dist/{serve-Cis4oJM-.js → serve-D5ijYGTv.js} +42 -28
- package/dist/{setup-B0sg4AV1.js → setup-DMJYufvD.js} +2 -2
- package/dist/{shareCmd-DKIvwe0I.js → shareCmd-CNhCKuOK.js} +3 -3
- package/dist/{subcommands-CvL_1Nt9.js → subcommands-BncoxUN6.js} +775 -78
- package/dist/subcommands-ZEjbTV_7.js +13 -0
- package/dist/{terminal-DqsAuqhw.js → terminal-DzbPI4n0.js} +2 -2
- package/dist/{trayApp-Dayj9Frx.js → trayApp-BvtgbxMB.js} +2 -2
- package/dist/trayApp-CpPsP3_a.js +5 -0
- package/dist/{ts-C2osm4MO.js → ts-Gr1eJOUk.js} +3 -3
- package/dist/{versionChecker-BmXwOe40.js → versionChecker-BrjeGNf1.js} +2 -2
- package/dist/{widget-D5tsB-oi.js → widget-p4Db_OXd.js} +3 -3
- package/dist/{ws-B4jdOeB2.js → ws-D_QClddd.js} +3 -3
- package/dist/{ws-mz7VJJwO.js → ws-DnKPi3TS.js} +2 -2
- package/package.json +1 -1
- package/ts/composerGuard.spec.ts +279 -0
- package/ts/composerGuard.ts +366 -0
- package/ts/globalPidIndex.ts +7 -0
- package/ts/messageLog.ts +2 -0
- package/ts/needsInput.bun.spec.ts +36 -0
- package/ts/needsInput.spec.ts +36 -0
- package/ts/remotes.bun.spec.ts +125 -0
- package/ts/remotes.spec.ts +125 -0
- package/ts/remotes.ts +95 -5
- package/ts/serve.bun.spec.ts +50 -0
- package/ts/serve.spec.ts +57 -1
- package/ts/serve.ts +50 -9
- package/ts/subcommands.spec.ts +564 -42
- package/ts/subcommands.ts +650 -79
- package/ts/workDir.bun.spec.ts +90 -0
- package/ts/workDir.spec.ts +90 -0
- package/ts/workDir.ts +75 -0
- package/dist/remotes-BDMnvGI6.js +0 -5
- package/dist/subcommands-DioyG80q.js +0 -13
- package/dist/trayApp-C6C7-TSh.js +0 -5
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { mkdtemp, rm } from "fs/promises";
|
|
2
|
+
import { tmpdir } from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { cmdRemote, MASK, maskRemoteUrl, writeRemoteAlias } from "./remotes.ts";
|
|
6
|
+
import { parseWebrtcLink } from "./webrtcLink.ts";
|
|
7
|
+
|
|
8
|
+
// Synthetic secrets only — a v2 share secret shape (e1.<64 hex>) and a plain token.
|
|
9
|
+
const SECRET = "e1.0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
10
|
+
const TOKEN = "tok-aaaaaaaabbbbbbbbcccccccc";
|
|
11
|
+
|
|
12
|
+
describe("maskRemoteUrl", () => {
|
|
13
|
+
it("masks the token of a webrtc:// link, keeping room and host", () => {
|
|
14
|
+
expect(maskRemoteUrl(`webrtc://room1:${SECRET}@s.example.com`)).toBe(
|
|
15
|
+
`webrtc://room1:${MASK}@s.example.com`,
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
it("masks a positional /w/ share link, with and without a sighost", () => {
|
|
19
|
+
expect(maskRemoteUrl(`https://agent-yes.com/w/#room1:${SECRET}`)).toBe(
|
|
20
|
+
`https://agent-yes.com/w/#room1:${MASK}`,
|
|
21
|
+
);
|
|
22
|
+
expect(maskRemoteUrl(`https://agent-yes.com/w/#room1:${SECRET}@s.example.com`)).toBe(
|
|
23
|
+
`https://agent-yes.com/w/#room1:${MASK}@s.example.com`,
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
it("masks s= and k= in a key=value fragment, keeping the other keys", () => {
|
|
27
|
+
expect(maskRemoteUrl(`https://agent-yes.com/room/#room=room1&s=${SECRET}&sig=s.x`)).toBe(
|
|
28
|
+
`https://agent-yes.com/room/#room=room1&s=${MASK}&sig=s.x`,
|
|
29
|
+
);
|
|
30
|
+
expect(maskRemoteUrl(`https://box.example.ts.net/ay/#k=${TOKEN}`)).toBe(
|
|
31
|
+
`https://box.example.ts.net/ay/#k=${MASK}`,
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
it("masks percent-encoded secret keys, which URLSearchParams decodes on use", () => {
|
|
35
|
+
expect(maskRemoteUrl(`https://agent-yes.com/room/#room=room1&%73=${SECRET}`)).toBe(
|
|
36
|
+
`https://agent-yes.com/room/#room=room1&%73=${MASK}`,
|
|
37
|
+
);
|
|
38
|
+
expect(maskRemoteUrl(`https://agent-yes.com/room/#%73=${SECRET}&room=room1`)).toBe(
|
|
39
|
+
`https://agent-yes.com/room/#%73=${MASK}&room=room1`,
|
|
40
|
+
);
|
|
41
|
+
expect(maskRemoteUrl(`https://box.example.ts.net/ay/#?%6B=${TOKEN}`)).toBe(
|
|
42
|
+
`https://box.example.ts.net/ay/#?%6B=${MASK}`,
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
it("treats a room id containing `=` as positional, like the parser does", () => {
|
|
46
|
+
expect(maskRemoteUrl(`https://agent-yes.com/w/#room1=demo:${SECRET}`)).toBe(
|
|
47
|
+
`https://agent-yes.com/w/#room1=demo:${MASK}`,
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
it("masks a malformed webrtc:// link that has no @host", () => {
|
|
51
|
+
expect(maskRemoteUrl(`webrtc://room1:${SECRET}`)).toBe(`webrtc://room1:${MASK}`);
|
|
52
|
+
});
|
|
53
|
+
it("masks userinfo credentials", () => {
|
|
54
|
+
expect(maskRemoteUrl(`http://${TOKEN}@192.0.2.5:7432`)).toBe(`http://${MASK}@192.0.2.5:7432`);
|
|
55
|
+
});
|
|
56
|
+
it("leaves a URL without secrets untouched", () => {
|
|
57
|
+
expect(maskRemoteUrl("http://192.0.2.5:7432")).toBe("http://192.0.2.5:7432");
|
|
58
|
+
expect(maskRemoteUrl("https://box.example.ts.net/ay")).toBe("https://box.example.ts.net/ay");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Cross-check against the real parser: whatever it accepts as a working share
|
|
63
|
+
// link, the masked form must not contain the secret it would connect with.
|
|
64
|
+
describe("maskRemoteUrl vs parseWebrtcLink", () => {
|
|
65
|
+
const HEX = SECRET.slice(3);
|
|
66
|
+
const links = [
|
|
67
|
+
`webrtc://room1:${SECRET}@s.example.com`,
|
|
68
|
+
`https://agent-yes.com/w/#room1:${SECRET}`,
|
|
69
|
+
`https://agent-yes.com/w/#room1:${SECRET}@s.example.com`,
|
|
70
|
+
`https://agent-yes.com/w/#room1=demo:${SECRET}`,
|
|
71
|
+
`https://agent-yes.com/room/#room=room1&s=${SECRET}`,
|
|
72
|
+
`https://agent-yes.com/room/#?room=room1&s=${SECRET}&sig=s.example.com`,
|
|
73
|
+
`https://agent-yes.com/room/#room=room1&%73=${SECRET}`,
|
|
74
|
+
`https://agent-yes.com/room/#s=${SECRET}&room=room1`,
|
|
75
|
+
];
|
|
76
|
+
for (const link of links) {
|
|
77
|
+
it(`hides the secret of ${link.replace(SECRET, "<secret>")}`, () => {
|
|
78
|
+
expect(parseWebrtcLink(link)?.s).toBe(HEX); // the parser accepts it
|
|
79
|
+
expect(maskRemoteUrl(link)).not.toContain(HEX.slice(0, 16));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("ay remote ls", () => {
|
|
85
|
+
let home: string;
|
|
86
|
+
let out: string;
|
|
87
|
+
let prevHome: string | undefined;
|
|
88
|
+
beforeEach(async () => {
|
|
89
|
+
home = await mkdtemp(path.join(tmpdir(), "ay-remotes-"));
|
|
90
|
+
prevHome = process.env.AGENT_YES_HOME;
|
|
91
|
+
process.env.AGENT_YES_HOME = home;
|
|
92
|
+
out = "";
|
|
93
|
+
vi.spyOn(process.stdout, "write").mockImplementation((c: any) => ((out += String(c)), true));
|
|
94
|
+
vi.spyOn(process.stderr, "write").mockImplementation(() => true);
|
|
95
|
+
await writeRemoteAlias("rtc", { url: `https://agent-yes.com/w/#room1:${SECRET}`, token: "" });
|
|
96
|
+
await writeRemoteAlias("lan", { url: "http://192.0.2.5:7432", token: TOKEN });
|
|
97
|
+
});
|
|
98
|
+
afterEach(async () => {
|
|
99
|
+
vi.restoreAllMocks();
|
|
100
|
+
if (prevHome === undefined) delete process.env.AGENT_YES_HOME;
|
|
101
|
+
else process.env.AGENT_YES_HOME = prevHome;
|
|
102
|
+
await rm(home, { recursive: true, force: true });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("masks every secret by default", async () => {
|
|
106
|
+
expect(await cmdRemote(["ls"])).toBe(0);
|
|
107
|
+
expect(out).not.toContain(SECRET);
|
|
108
|
+
expect(out).not.toContain(TOKEN.slice(0, 8));
|
|
109
|
+
expect(out).toContain(`rtc\thttps://agent-yes.com/w/#room1:${MASK}\n`);
|
|
110
|
+
expect(out).toContain(`lan\thttp://192.0.2.5:7432\ttoken:${MASK}\n`);
|
|
111
|
+
});
|
|
112
|
+
it("reveals secrets with --show-secrets", async () => {
|
|
113
|
+
expect(await cmdRemote(["ls", "--show-secrets"])).toBe(0);
|
|
114
|
+
expect(out).toContain(SECRET);
|
|
115
|
+
expect(out).toContain(`token:${TOKEN}`);
|
|
116
|
+
});
|
|
117
|
+
it("accepts the flag without the ls verb", async () => {
|
|
118
|
+
expect(await cmdRemote(["--show-secrets"])).toBe(0);
|
|
119
|
+
expect(out).toContain(SECRET);
|
|
120
|
+
});
|
|
121
|
+
it("rejects an unknown flag instead of ignoring it", async () => {
|
|
122
|
+
expect(await cmdRemote(["ls", "--show-secret"])).toBe(1);
|
|
123
|
+
expect(out).toBe("");
|
|
124
|
+
});
|
|
125
|
+
});
|
package/ts/remotes.ts
CHANGED
|
@@ -175,6 +175,81 @@ async function resolveWebrtc(
|
|
|
175
175
|
return { url: bridge.baseUrl, token: bridge.token, keyword, label };
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// secret masking for display
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
/** What a secret is replaced with in `ay remote ls` / `add` output. */
|
|
183
|
+
export const MASK = "***";
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A saved remote URL with every embedded secret replaced by MASK, so listing
|
|
187
|
+
* remotes doesn't print a usable credential into a terminal, a log, or an
|
|
188
|
+
* agent's transcript. Covers every form `ay remote add` stores:
|
|
189
|
+
* webrtc://<room>:<token>@<host> → webrtc://<room>:***@<host>
|
|
190
|
+
* https://…/w/#<room>:<token>[@<sighost>] → https://…/w/#<room>:***[@<sighost>]
|
|
191
|
+
* https://…/room/#room=<id>&s=<token>[&sig=…] → …#room=<id>&s=***[&sig=…]
|
|
192
|
+
* https://…/#k=<token> → https://…/#k=***
|
|
193
|
+
* http://<token>@host:port → http://***@host:port
|
|
194
|
+
* The room id is kept: it is shown elsewhere as the remote's label and is not
|
|
195
|
+
* enough to join without the token.
|
|
196
|
+
*/
|
|
197
|
+
export function maskRemoteUrl(url: string): string {
|
|
198
|
+
if (/^webrtc:\/\//i.test(url)) {
|
|
199
|
+
// Same grammar as parseRaw (token runs to the `@`); a malformed link with no
|
|
200
|
+
// `@` still has everything after the room masked rather than printed.
|
|
201
|
+
return url.replace(/^(webrtc:\/\/[^:]*:)[^@]*/i, `$1${MASK}`);
|
|
202
|
+
}
|
|
203
|
+
if (!/^https?:\/\//i.test(url)) return url;
|
|
204
|
+
const hashAt = url.indexOf("#");
|
|
205
|
+
// userinfo (`http://<token>@host` or `http://user:pass@host`) — before the path.
|
|
206
|
+
const base = (hashAt >= 0 ? url.slice(0, hashAt) : url).replace(
|
|
207
|
+
/^(https?:\/\/)[^/@]+@/i,
|
|
208
|
+
`$1${MASK}@`,
|
|
209
|
+
);
|
|
210
|
+
if (hashAt < 0) return base;
|
|
211
|
+
const frag = url.slice(hashAt + 1);
|
|
212
|
+
// Classify exactly as the connection parser does (parseFragment in
|
|
213
|
+
// webrtcLink.ts): key=value only when it starts with a plain `<letters>=`;
|
|
214
|
+
// otherwise positional #<room>:<token>[@<sighost>]. A fragment that is
|
|
215
|
+
// neither (e.g. an encoded `#%73=…`) still gets its secret keys masked.
|
|
216
|
+
if (/^\??[A-Za-z_]+=/.test(frag)) return `${base}#${maskFragmentParams(frag)}`;
|
|
217
|
+
if (/^[^:@]*:/.test(frag)) return `${base}#${frag.replace(/^([^:@]*:)[^@]*/, `$1${MASK}`)}`;
|
|
218
|
+
return `${base}#${maskFragmentParams(frag)}`;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Secret-bearing fragment keys: `s` (room secret) and `k` (`ay share` token). */
|
|
222
|
+
const SECRET_KEYS = new Set(["s", "k"]);
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Mask the values of SECRET_KEYS in a key=value fragment. Keys are compared
|
|
226
|
+
* after the same decoding URLSearchParams applies when the link is USED
|
|
227
|
+
* (`%73=` is `s=`, `+` is a space), so an encoded spelling can't slip a
|
|
228
|
+
* working secret past the mask.
|
|
229
|
+
*/
|
|
230
|
+
function maskFragmentParams(frag: string): string {
|
|
231
|
+
const lead = frag.startsWith("?") ? "?" : "";
|
|
232
|
+
return (
|
|
233
|
+
lead +
|
|
234
|
+
frag
|
|
235
|
+
.slice(lead.length)
|
|
236
|
+
.split("&")
|
|
237
|
+
.map((part) => {
|
|
238
|
+
const eq = part.indexOf("=");
|
|
239
|
+
const rawKey = eq >= 0 ? part.slice(0, eq) : part;
|
|
240
|
+
const key = decodeMaybe(rawKey.replace(/\+/g, " ")).trim().toLowerCase();
|
|
241
|
+
// `trim`/`toLowerCase` over-match on purpose: masking too much is harmless.
|
|
242
|
+
return SECRET_KEYS.has(key) ? `${rawKey}=${MASK}` : part;
|
|
243
|
+
})
|
|
244
|
+
.join("&")
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** A bare token for display: MASK, or the token itself with --show-secrets. */
|
|
249
|
+
function displayToken(token: string, showSecrets: boolean): string {
|
|
250
|
+
return showSecrets ? token : MASK;
|
|
251
|
+
}
|
|
252
|
+
|
|
178
253
|
// ---------------------------------------------------------------------------
|
|
179
254
|
// ay remote subcommand
|
|
180
255
|
// ---------------------------------------------------------------------------
|
|
@@ -187,7 +262,8 @@ export async function cmdRemote(rest: string[]): Promise<number> {
|
|
|
187
262
|
`Usage: ay remote <subcommand>\n\n` +
|
|
188
263
|
`Manage saved remote server aliases.\n\n` +
|
|
189
264
|
`Subcommands:\n` +
|
|
190
|
-
` ay remote ls
|
|
265
|
+
` ay remote ls [--show-secrets] list configured remotes\n` +
|
|
266
|
+
` (tokens are masked unless --show-secrets)\n` +
|
|
191
267
|
` ay remote add <alias> http://<token>@<host>:<port> add an http remote\n` +
|
|
192
268
|
` ay remote add <alias> webrtc://<room>:<token>@<host> add a WebRTC share remote\n` +
|
|
193
269
|
` ay remote add <alias> https://agent-yes.com/w/#<room>:<token> (share link form)\n` +
|
|
@@ -201,7 +277,16 @@ export async function cmdRemote(rest: string[]): Promise<number> {
|
|
|
201
277
|
return 0;
|
|
202
278
|
}
|
|
203
279
|
|
|
204
|
-
if (!sub || sub === "ls" || sub === "list") {
|
|
280
|
+
if (!sub || sub === "ls" || sub === "list" || sub.startsWith("-")) {
|
|
281
|
+
const flags = sub && !sub.startsWith("-") ? rest.slice(1) : rest;
|
|
282
|
+
const unknown = flags.filter((f) => f !== "--show-secrets");
|
|
283
|
+
if (unknown.length) {
|
|
284
|
+
process.stderr.write(
|
|
285
|
+
`ay remote ls: unknown argument '${unknown[0]}' (only --show-secrets)\n`,
|
|
286
|
+
);
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
const showSecrets = flags.includes("--show-secrets");
|
|
205
290
|
const remotes = await readRemotes();
|
|
206
291
|
if (remotes.size === 0) {
|
|
207
292
|
process.stdout.write("no remotes configured\n");
|
|
@@ -212,10 +297,15 @@ export async function cmdRemote(rest: string[]): Promise<number> {
|
|
|
212
297
|
);
|
|
213
298
|
return 0;
|
|
214
299
|
}
|
|
300
|
+
let masked = false;
|
|
215
301
|
for (const [alias, cfg] of remotes) {
|
|
216
|
-
const
|
|
217
|
-
|
|
302
|
+
const url = showSecrets ? cfg.url : maskRemoteUrl(cfg.url);
|
|
303
|
+
// WebRTC remotes keep their secret inside the link and store no token.
|
|
304
|
+
const token = cfg.token ? `\ttoken:${displayToken(cfg.token, showSecrets)}` : "";
|
|
305
|
+
masked ||= url !== cfg.url || (!!cfg.token && !showSecrets);
|
|
306
|
+
process.stdout.write(`${alias}\t${url}${token}\n`);
|
|
218
307
|
}
|
|
308
|
+
if (masked) process.stderr.write(`\n ay remote ls --show-secrets # reveal tokens\n`);
|
|
219
309
|
return 0;
|
|
220
310
|
}
|
|
221
311
|
|
|
@@ -231,7 +321,7 @@ export async function cmdRemote(rest: string[]): Promise<number> {
|
|
|
231
321
|
// WebRTC share links carry their own secret — store verbatim (token in the link).
|
|
232
322
|
if (isWebrtcSpec(rawUrl)) {
|
|
233
323
|
await writeRemoteAlias(alias, { url: rawUrl, token: "" });
|
|
234
|
-
process.stdout.write(`remote '${alias}' added → ${rawUrl} (webrtc)\n`);
|
|
324
|
+
process.stdout.write(`remote '${alias}' added → ${maskRemoteUrl(rawUrl)} (webrtc)\n`);
|
|
235
325
|
process.stderr.write(`\n ay ls ${alias} # list agents on ${alias}\n`);
|
|
236
326
|
return 0;
|
|
237
327
|
}
|
package/ts/serve.bun.spec.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { readFile } from "fs/promises";
|
|
|
2
2
|
import { describe, expect, it } from "bun:test";
|
|
3
3
|
import {
|
|
4
4
|
installerArgv,
|
|
5
|
+
probeDaemon,
|
|
5
6
|
isNoNodeExecError,
|
|
6
7
|
oxmgrVersionHasWindowsFix,
|
|
7
8
|
parseGitUrl,
|
|
@@ -164,3 +165,52 @@ describe("parseGitUrl", () => {
|
|
|
164
165
|
expect(parseGitUrl("file:///etc/passwd")).toBeNull();
|
|
165
166
|
});
|
|
166
167
|
});
|
|
168
|
+
|
|
169
|
+
// `ay serve status` reported a LISTENING, answering daemon as "not running"
|
|
170
|
+
// because the probe's 3s budget expired on an overloaded host — a no-op
|
|
171
|
+
// /api/version measured 1.1s there. A timeout is not an absence, and the two
|
|
172
|
+
// want opposite responses (wait vs reinstall), so they must not collapse.
|
|
173
|
+
// node:net / node:http here, not Bun.*: this suite runs under BOTH runtimes.
|
|
174
|
+
describe("probeDaemon", () => {
|
|
175
|
+
const listen = async (srv: { listen: (p: number, h: string, cb: () => void) => void }) => {
|
|
176
|
+
await new Promise<void>((r) => srv.listen(0, "127.0.0.1", r));
|
|
177
|
+
return (srv as unknown as { address(): { port: number } }).address().port;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
it("reports down when nothing is listening on the port", async () => {
|
|
181
|
+
const { createServer } = await import("node:net");
|
|
182
|
+
const srv = createServer();
|
|
183
|
+
const port = await listen(srv);
|
|
184
|
+
await new Promise<void>((r) => srv.close(() => r())); // free it, so the port refuses
|
|
185
|
+
expect((await probeDaemon(port, "tok", 2000)).kind).toBe("down");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("reports SLOW, not down, for a port that accepts but never answers", async () => {
|
|
189
|
+
const { createServer } = await import("node:net");
|
|
190
|
+
const srv = createServer(() => {}); // accept, then hold — an overloaded daemon
|
|
191
|
+
const port = await listen(srv);
|
|
192
|
+
try {
|
|
193
|
+
expect((await probeDaemon(port, "tok", 250)).kind).toBe("slow");
|
|
194
|
+
} finally {
|
|
195
|
+
srv.close();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("reports the version when the daemon answers", async () => {
|
|
200
|
+
const { createServer } = await import("node:http");
|
|
201
|
+
const srv = createServer((_req, res) => {
|
|
202
|
+
res.setHeader("content-type", "application/json");
|
|
203
|
+
res.end(JSON.stringify({ version: "9.9.9" }));
|
|
204
|
+
});
|
|
205
|
+
const port = await listen(srv);
|
|
206
|
+
try {
|
|
207
|
+
expect(await probeDaemon(port, "tok", 5000)).toEqual({ kind: "ok", version: "9.9.9" });
|
|
208
|
+
} finally {
|
|
209
|
+
srv.close();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("treats a null port as down without dialling", async () => {
|
|
214
|
+
expect((await probeDaemon(null, "tok", 1)).kind).toBe("down");
|
|
215
|
+
});
|
|
216
|
+
});
|
package/ts/serve.spec.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { readFile } from "fs/promises";
|
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
import {
|
|
4
4
|
installerArgv,
|
|
5
|
+
probeDaemon,
|
|
5
6
|
isNoNodeExecError,
|
|
6
7
|
oxmgrCmd,
|
|
7
8
|
oxmgrVersionHasWindowsFix,
|
|
@@ -168,6 +169,58 @@ describe("parseGitUrl", () => {
|
|
|
168
169
|
});
|
|
169
170
|
});
|
|
170
171
|
|
|
172
|
+
// `ay serve status` reported a LISTENING, answering daemon as "not running"
|
|
173
|
+
// because the probe's 3s budget expired on an overloaded host — a no-op
|
|
174
|
+
// /api/version measured 1.1s there. A timeout is not an absence, and the two
|
|
175
|
+
// want opposite responses (wait vs reinstall), so they must not collapse.
|
|
176
|
+
// node:net / node:http here, not Bun.*: this suite runs under BOTH runtimes.
|
|
177
|
+
describe("probeDaemon", () => {
|
|
178
|
+
const listen = async (srv: { listen: (p: number, h: string, cb: () => void) => void }) => {
|
|
179
|
+
await new Promise<void>((r) => srv.listen(0, "127.0.0.1", r));
|
|
180
|
+
return (srv as unknown as { address(): { port: number } }).address().port;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
it("reports down when nothing is listening on the port", async () => {
|
|
184
|
+
const { createServer } = await import("node:net");
|
|
185
|
+
const srv = createServer();
|
|
186
|
+
const port = await listen(srv);
|
|
187
|
+
await new Promise<void>((r) => srv.close(() => r())); // free it, so the port refuses
|
|
188
|
+
expect((await probeDaemon(port, "tok", 2000)).kind).toBe("down");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("reports SLOW, not down, for a port that accepts but never answers", async () => {
|
|
192
|
+
const { createServer } = await import("node:net");
|
|
193
|
+
const srv = createServer(() => {}); // accept, then hold — an overloaded daemon
|
|
194
|
+
const port = await listen(srv);
|
|
195
|
+
try {
|
|
196
|
+
expect((await probeDaemon(port, "tok", 250)).kind).toBe("slow");
|
|
197
|
+
} finally {
|
|
198
|
+
srv.close();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("reports the version when the daemon answers", async () => {
|
|
203
|
+
const { createServer } = await import("node:http");
|
|
204
|
+
const srv = createServer((_req, res) => {
|
|
205
|
+
res.setHeader("content-type", "application/json");
|
|
206
|
+
res.end(JSON.stringify({ version: "9.9.9" }));
|
|
207
|
+
});
|
|
208
|
+
const port = await listen(srv);
|
|
209
|
+
try {
|
|
210
|
+
expect(await probeDaemon(port, "tok", 5000)).toEqual({
|
|
211
|
+
kind: "ok",
|
|
212
|
+
version: "9.9.9",
|
|
213
|
+
});
|
|
214
|
+
} finally {
|
|
215
|
+
srv.close();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("treats a null port as down without dialling", async () => {
|
|
220
|
+
expect((await probeDaemon(null, "tok", 1)).kind).toBe("down");
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
171
224
|
// `ay serve status` reads ~/.agent-yes/.share-link, which outlives the daemon
|
|
172
225
|
// config that minted it: reinstalling without --share leaves the file behind
|
|
173
226
|
// while nothing hosts the room. The link must then be flagged stale, not shown
|
|
@@ -184,7 +237,10 @@ describe("shareLinkStatus", () => {
|
|
|
184
237
|
expect(shareLinkStatus(link, false, false)).toEqual({ link, stale: false });
|
|
185
238
|
});
|
|
186
239
|
it("returns no link when the file is absent", () => {
|
|
187
|
-
expect(shareLinkStatus(null, true, false)).toEqual({
|
|
240
|
+
expect(shareLinkStatus(null, true, false)).toEqual({
|
|
241
|
+
link: null,
|
|
242
|
+
stale: false,
|
|
243
|
+
});
|
|
188
244
|
});
|
|
189
245
|
});
|
|
190
246
|
|
package/ts/serve.ts
CHANGED
|
@@ -1211,20 +1211,48 @@ function explicitWebrtcUrl(args: string[]): string | undefined {
|
|
|
1211
1211
|
// Ask the live daemon its version over the local HTTP API. null if it's not
|
|
1212
1212
|
// listening (webrtc-only) or too old to expose /api/version — both of which we
|
|
1213
1213
|
// treat as "outdated" so a re-install rolls it forward.
|
|
1214
|
-
|
|
1215
|
-
|
|
1214
|
+
/** Why a version probe came back empty. "slow" is NOT "down" — see probeDaemon. */
|
|
1215
|
+
export type DaemonProbe =
|
|
1216
|
+
| { kind: "ok"; version: string | null }
|
|
1217
|
+
| { kind: "slow" }
|
|
1218
|
+
| { kind: "down" };
|
|
1219
|
+
|
|
1220
|
+
// A loaded box answers /api/version in seconds, not milliseconds: the endpoint
|
|
1221
|
+
// does almost nothing, but the process still has to be scheduled and paged in.
|
|
1222
|
+
// Measured on this host under memory pressure: 1.1s for a no-op call. The old 3s
|
|
1223
|
+
// budget turned that into `ay serve status` reporting "not reachable … (not
|
|
1224
|
+
// running)" for a daemon that lsof showed LISTENING and answering — the most
|
|
1225
|
+
// misleading moment possible, since a slow box is exactly when someone runs
|
|
1226
|
+
// status. A refused connection fails instantly regardless, so a longer deadline
|
|
1227
|
+
// only costs time in the case where waiting is the right answer.
|
|
1228
|
+
const DAEMON_PROBE_TIMEOUT_MS = 15_000;
|
|
1229
|
+
|
|
1230
|
+
export async function probeDaemon(
|
|
1231
|
+
port: number | null,
|
|
1232
|
+
token: string,
|
|
1233
|
+
timeoutMs: number = DAEMON_PROBE_TIMEOUT_MS,
|
|
1234
|
+
): Promise<DaemonProbe> {
|
|
1235
|
+
if (port === null) return { kind: "down" };
|
|
1216
1236
|
try {
|
|
1217
1237
|
const r = await fetch(`http://127.0.0.1:${port}/api/version`, {
|
|
1218
1238
|
headers: { Authorization: `Bearer ${token}` },
|
|
1219
|
-
signal: AbortSignal.timeout(
|
|
1239
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
1220
1240
|
});
|
|
1221
|
-
if (!r.ok) return
|
|
1222
|
-
return ((await r.json()) as { version?: string }).version ?? null;
|
|
1223
|
-
} catch {
|
|
1224
|
-
|
|
1241
|
+
if (!r.ok) return { kind: "down" };
|
|
1242
|
+
return { kind: "ok", version: ((await r.json()) as { version?: string }).version ?? null };
|
|
1243
|
+
} catch (e) {
|
|
1244
|
+
// AbortSignal.timeout aborts with a TimeoutError; anything else (ECONNREFUSED,
|
|
1245
|
+
// EHOSTUNREACH, a socket hangup) means nothing is listening there.
|
|
1246
|
+
const name = (e as { name?: string } | null)?.name;
|
|
1247
|
+
return name === "TimeoutError" || name === "AbortError" ? { kind: "slow" } : { kind: "down" };
|
|
1225
1248
|
}
|
|
1226
1249
|
}
|
|
1227
1250
|
|
|
1251
|
+
async function fetchDaemonVersion(port: number | null, token: string): Promise<string | null> {
|
|
1252
|
+
const p = await probeDaemon(port, token);
|
|
1253
|
+
return p.kind === "ok" ? p.version : null;
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1228
1256
|
// ay serve start | stop — control the ALREADY-INSTALLED daemon's run state via
|
|
1229
1257
|
// the manager. Used by the tray's toggle and available on the CLI. Starting an
|
|
1230
1258
|
// unregistered daemon is a no-op with a hint to `install`.
|
|
@@ -1620,7 +1648,9 @@ async function cmdServeStatus(args: string[]): Promise<number> {
|
|
|
1620
1648
|
|
|
1621
1649
|
// Probe the local HTTP API — catches both a daemon and a foreground `ay serve`.
|
|
1622
1650
|
// Webrtc-only servers open no port, so a null probe there is expected, not down.
|
|
1623
|
-
const
|
|
1651
|
+
const probe: DaemonProbe =
|
|
1652
|
+
httpish && token ? await probeDaemon(port, token) : { kind: "down" as const };
|
|
1653
|
+
const runningVersion = probe.kind === "ok" ? probe.version : null;
|
|
1624
1654
|
const current = getInstalledPackage().version;
|
|
1625
1655
|
const share = shareLinkStatus(shareLink, installed, webrtcish);
|
|
1626
1656
|
|
|
@@ -1635,7 +1665,12 @@ async function cmdServeStatus(args: string[]): Promise<number> {
|
|
|
1635
1665
|
port: httpish ? port : null,
|
|
1636
1666
|
localUrl: httpish ? localUrl : null,
|
|
1637
1667
|
reachable: runningVersion !== null,
|
|
1668
|
+
// Additive: `reachable:false` alone cannot tell a caller whether the
|
|
1669
|
+
// daemon is gone or merely slow, and those want opposite responses
|
|
1670
|
+
// (reinstall vs wait). Existing consumers of `reachable` are unaffected.
|
|
1671
|
+
probe: probe.kind,
|
|
1638
1672
|
runningVersion,
|
|
1673
|
+
|
|
1639
1674
|
currentVersion: current,
|
|
1640
1675
|
upToDate: runningVersion !== null && runningVersion === current,
|
|
1641
1676
|
args: a,
|
|
@@ -1671,8 +1706,14 @@ async function cmdServeStatus(args: string[]): Promise<number> {
|
|
|
1671
1706
|
} else if (mode === "webrtc") {
|
|
1672
1707
|
w(`http api: none (webrtc-only)`);
|
|
1673
1708
|
} else {
|
|
1709
|
+
const where = local ? `via ${localUrl}` : `on 127.0.0.1:${port}`;
|
|
1710
|
+
// A timeout is not an absence. Saying "not running" for a daemon that is
|
|
1711
|
+
// listening but slow sends the reader off to reinstall something that was
|
|
1712
|
+
// never broken; name the machine instead.
|
|
1674
1713
|
w(
|
|
1675
|
-
|
|
1714
|
+
probe.kind === "slow"
|
|
1715
|
+
? `http api: listening ${where} but did not answer within ${DAEMON_PROBE_TIMEOUT_MS / 1000}s — the daemon is up and the host is overloaded, not down`
|
|
1716
|
+
: `http api: not reachable ${where} (not running)`,
|
|
1676
1717
|
);
|
|
1677
1718
|
}
|
|
1678
1719
|
w(`token: ${token ?? "(none yet — created on first serve)"}`);
|