@x-tech-laboratories/x-app 0.129.0 → 0.130.1
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 +11 -2
- package/dist/agent/claude_stream.d.ts +1 -22
- package/dist/agent/claude_stream.js +2 -85
- package/dist/agent/client.d.ts +1 -35
- package/dist/agent/client.js +2 -169
- package/dist/agent/commands.d.ts +1 -22
- package/dist/agent/commands.js +2 -80
- package/dist/agent/desktop_notify.d.ts +1 -54
- package/dist/agent/desktop_notify.js +2 -229
- package/dist/agent/firefox_bookmarks.d.ts +1 -29
- package/dist/agent/firefox_bookmarks.js +2 -147
- package/dist/agent/git.d.ts +1 -37
- package/dist/agent/git.js +2 -121
- package/dist/agent/github.d.ts +1 -81
- package/dist/agent/github.js +2 -185
- package/dist/agent/inhibit.d.ts +1 -32
- package/dist/agent/inhibit.js +2 -141
- package/dist/agent/keys.d.ts +1 -26
- package/dist/agent/keys.js +2 -161
- package/dist/agent/local_socket.d.ts +1 -18
- package/dist/agent/local_socket.js +2 -106
- package/dist/agent/long_runs.d.ts +1 -45
- package/dist/agent/long_runs.js +2 -419
- package/dist/agent/programs.d.ts +1 -60
- package/dist/agent/programs.js +2 -237
- package/dist/agent/protocol.d.ts +1 -172
- package/dist/agent/protocol.js +2 -249
- package/dist/agent/scheduled_notices.d.ts +1 -21
- package/dist/agent/scheduled_notices.js +2 -38
- package/dist/agent/session.d.ts +1 -61
- package/dist/agent/session.js +2 -335
- package/dist/agent/ssh_config.d.ts +1 -12
- package/dist/agent/ssh_config.js +2 -72
- package/dist/agent/stalls.d.ts +1 -44
- package/dist/agent/stalls.js +2 -176
- package/dist/agent/tasks.d.ts +1 -17
- package/dist/agent/tasks.js +2 -15
- package/dist/agent/wake.d.ts +1 -14
- package/dist/agent/wake.js +2 -88
- package/dist/agent/worktree_files.d.ts +1 -20
- package/dist/agent/worktree_files.js +2 -69
- package/dist/app/desktop.js +21 -8
- package/dist/daemon_link.d.ts +2 -1
- package/dist/daemon_link.js +4 -2
- package/dist/durations.d.ts +1 -15
- package/dist/durations.js +2 -52
- package/package.json +5 -3
|
@@ -1,38 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// stopped still shows when it starts, up to half a day late.
|
|
4
|
-
// ── Checks ───────────────────────────────────────────────────────────────────
|
|
5
|
-
const half_a_day_ms = 12 * 3600 * 1000;
|
|
6
|
-
// Checks at once and then on a timer; returns how to stop.
|
|
7
|
-
export function keep_giving_scheduled_notices(options) {
|
|
8
|
-
const now = options.now ?? Date.now;
|
|
9
|
-
const report = options.on_error ?? (() => { });
|
|
10
|
-
const started = now();
|
|
11
|
-
const kept = options.read_checked_until?.() ?? null;
|
|
12
|
-
let checked_until = kept === null ? started : Math.max(kept, started - (options.most_late_ms ?? half_a_day_ms));
|
|
13
|
-
function check() {
|
|
14
|
-
const until = now();
|
|
15
|
-
let notices;
|
|
16
|
-
try {
|
|
17
|
-
notices = options.due(checked_until, until);
|
|
18
|
-
}
|
|
19
|
-
catch (error) {
|
|
20
|
-
report(error);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
checked_until = until;
|
|
24
|
-
for (const notice of notices) {
|
|
25
|
-
try {
|
|
26
|
-
void Promise.resolve(options.notifier.show(notice)).catch(report);
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
report(error);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (notices.length > 0)
|
|
33
|
-
options.write_checked_until?.(until);
|
|
34
|
-
}
|
|
35
|
-
check();
|
|
36
|
-
const timer = setInterval(check, options.check_ms ?? 30_000);
|
|
37
|
-
return () => clearInterval(timer);
|
|
38
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/scheduled_notices";
|
package/dist/agent/session.d.ts
CHANGED
|
@@ -1,61 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { LongRuns } from "./long_runs.ts";
|
|
3
|
-
import { type AgentApp, type AgentMessage, type AppMessage } from "./protocol.ts";
|
|
4
|
-
import type { TaskRunner } from "./tasks.ts";
|
|
5
|
-
export type AppRequests = Record<string, (message: AppMessage) => unknown>;
|
|
6
|
-
export interface SessionHooks<Task> {
|
|
7
|
-
requests?: AppRequests;
|
|
8
|
-
on_message?: (message: AppMessage, session: AgentSession<Task>) => void;
|
|
9
|
-
on_hello?: (session: AgentSession<Task>) => void;
|
|
10
|
-
}
|
|
11
|
-
export interface SessionOptions<Task> extends SessionHooks<Task> {
|
|
12
|
-
app: AgentApp<Task>;
|
|
13
|
-
runner: TaskRunner<Task>;
|
|
14
|
-
host: string;
|
|
15
|
-
agent_version: string;
|
|
16
|
-
heartbeat_in_milliseconds: number;
|
|
17
|
-
key?: KeyObject | null;
|
|
18
|
-
server_keys?: ReadonlyMap<string, KeyObject>;
|
|
19
|
-
challenge_timeout_in_milliseconds?: number;
|
|
20
|
-
log?: (line: string) => void;
|
|
21
|
-
}
|
|
22
|
-
export declare const server_refused_close_code = 4009;
|
|
23
|
-
export interface MessageSocket {
|
|
24
|
-
readonly readyState: number;
|
|
25
|
-
send(data: string | Uint8Array<ArrayBuffer>): void;
|
|
26
|
-
close(code?: number, reason?: string): void;
|
|
27
|
-
}
|
|
28
|
-
export declare class AgentSession<Task> {
|
|
29
|
-
private readonly socket;
|
|
30
|
-
private readonly runs;
|
|
31
|
-
private readonly options;
|
|
32
|
-
private readonly pumps;
|
|
33
|
-
private last_heard;
|
|
34
|
-
private readonly heartbeat;
|
|
35
|
-
private closed;
|
|
36
|
-
private greeted;
|
|
37
|
-
private readonly checks_server;
|
|
38
|
-
private signed;
|
|
39
|
-
private challenge_timer;
|
|
40
|
-
constructor(socket: MessageSocket, runs: LongRuns<Task>, options: SessionOptions<Task>);
|
|
41
|
-
private say_hello;
|
|
42
|
-
receive(text: string): void;
|
|
43
|
-
close(): void;
|
|
44
|
-
send(message: AgentMessage | AppMessage): void;
|
|
45
|
-
send_chunk(id: string, data: Buffer): void;
|
|
46
|
-
end_stream(id: string, result: {
|
|
47
|
-
exit_code: number;
|
|
48
|
-
stderr: string;
|
|
49
|
-
}): void;
|
|
50
|
-
private receive_challenge;
|
|
51
|
-
private open_signed;
|
|
52
|
-
private refuse;
|
|
53
|
-
private dispatch;
|
|
54
|
-
private handle;
|
|
55
|
-
private handle_app;
|
|
56
|
-
private start_pump;
|
|
57
|
-
private answer;
|
|
58
|
-
private open_stream;
|
|
59
|
-
private value_for;
|
|
60
|
-
private beat;
|
|
61
|
-
}
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/session";
|
package/dist/agent/session.js
CHANGED
|
@@ -1,335 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// streams it was sending; long runs carry on. Given the server keys it accepts, it checks the server's signed challenge
|
|
4
|
-
// before it says anything, and then takes only messages the server signed for this connection, in order, refusing and
|
|
5
|
-
// hanging up on any other.
|
|
6
|
-
import { agent_hello_text, new_nonce, server_challenge_text, server_message_text, sign_challenge, sign_text, verify_text } from "./keys.js";
|
|
7
|
-
import { encode_chunk, encode_message, is_server_message, most_runs_in_hello, parse_server_message, parse_signed_message, } from "./protocol.js";
|
|
8
|
-
// The close code an agent hangs up with when the server's challenge or a message is not signed as it must be.
|
|
9
|
-
export const server_refused_close_code = 4009;
|
|
10
|
-
const socket_open = 1;
|
|
11
|
-
// ── Output ───────────────────────────────────────────────────────────────────
|
|
12
|
-
// Sends a stream's stdout as binary frames within the credit granted, then its end once all of it has gone.
|
|
13
|
-
class OutputPump {
|
|
14
|
-
id;
|
|
15
|
-
stream;
|
|
16
|
-
session;
|
|
17
|
-
credit;
|
|
18
|
-
pending = [];
|
|
19
|
-
stdout_ended = false;
|
|
20
|
-
result = null;
|
|
21
|
-
finished = false;
|
|
22
|
-
constructor(id, stream, window, session) {
|
|
23
|
-
this.id = id;
|
|
24
|
-
this.stream = stream;
|
|
25
|
-
this.credit = window;
|
|
26
|
-
this.session = session;
|
|
27
|
-
stream.stdout.on("data", (chunk) => {
|
|
28
|
-
this.pending.push(chunk);
|
|
29
|
-
this.flush();
|
|
30
|
-
});
|
|
31
|
-
stream.stdout.on("error", () => { });
|
|
32
|
-
stream.stdout.on("close", () => {
|
|
33
|
-
this.stdout_ended = true;
|
|
34
|
-
this.finish_when_done();
|
|
35
|
-
});
|
|
36
|
-
void stream.done.then((result) => {
|
|
37
|
-
this.result = result;
|
|
38
|
-
this.finish_when_done();
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
grant(bytes) {
|
|
42
|
-
this.credit += Math.max(0, bytes);
|
|
43
|
-
this.flush();
|
|
44
|
-
}
|
|
45
|
-
// Stops the command and drops what it had yet to send.
|
|
46
|
-
cancel() {
|
|
47
|
-
this.pending.length = 0;
|
|
48
|
-
this.stream.stop();
|
|
49
|
-
this.stream.stdout.resume();
|
|
50
|
-
}
|
|
51
|
-
flush() {
|
|
52
|
-
while (this.credit > 0 && this.pending.length > 0) {
|
|
53
|
-
const chunk = this.pending[0];
|
|
54
|
-
const piece = chunk.length > this.credit ? chunk.subarray(0, this.credit) : chunk;
|
|
55
|
-
if (piece === chunk)
|
|
56
|
-
this.pending.shift();
|
|
57
|
-
else
|
|
58
|
-
this.pending[0] = chunk.subarray(piece.length);
|
|
59
|
-
this.credit -= piece.length;
|
|
60
|
-
this.session.send_chunk(this.id, piece);
|
|
61
|
-
}
|
|
62
|
-
if (this.pending.length > 0)
|
|
63
|
-
this.stream.stdout.pause();
|
|
64
|
-
else if (!this.stdout_ended)
|
|
65
|
-
this.stream.stdout.resume();
|
|
66
|
-
this.finish_when_done();
|
|
67
|
-
}
|
|
68
|
-
finish_when_done() {
|
|
69
|
-
if (this.finished || !this.stdout_ended || this.result === null || this.pending.length > 0)
|
|
70
|
-
return;
|
|
71
|
-
this.finished = true;
|
|
72
|
-
this.session.end_stream(this.id, this.result);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
// ── Session ──────────────────────────────────────────────────────────────────
|
|
76
|
-
export class AgentSession {
|
|
77
|
-
socket;
|
|
78
|
-
runs;
|
|
79
|
-
options;
|
|
80
|
-
pumps = new Map();
|
|
81
|
-
last_heard = Date.now();
|
|
82
|
-
heartbeat;
|
|
83
|
-
closed = false;
|
|
84
|
-
greeted = false;
|
|
85
|
-
checks_server;
|
|
86
|
-
signed = null;
|
|
87
|
-
challenge_timer;
|
|
88
|
-
constructor(socket, runs, options) {
|
|
89
|
-
this.socket = socket;
|
|
90
|
-
this.runs = runs;
|
|
91
|
-
this.options = options;
|
|
92
|
-
this.heartbeat = setInterval(() => this.beat(), options.heartbeat_in_milliseconds);
|
|
93
|
-
this.heartbeat.unref();
|
|
94
|
-
this.checks_server = (options.server_keys?.size ?? 0) > 0;
|
|
95
|
-
if (this.checks_server) {
|
|
96
|
-
const timeout = options.challenge_timeout_in_milliseconds ?? 10_000;
|
|
97
|
-
this.challenge_timer = setTimeout(() => this.refuse("No signed challenge came from the server; it may hold no server key, so this agent will not answer it.", null), timeout);
|
|
98
|
-
this.challenge_timer.unref();
|
|
99
|
-
}
|
|
100
|
-
else if (!options.key) {
|
|
101
|
-
this.say_hello();
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
say_hello(signature, agent_nonce) {
|
|
105
|
-
this.greeted = true;
|
|
106
|
-
const hello = { type: "hello", protocol: this.options.app.protocol_version, host: this.options.host, agent_version: this.options.agent_version, runs: this.runs.list().slice(-most_runs_in_hello) };
|
|
107
|
-
this.send({ ...hello, ...(signature === undefined ? {} : { signature }), ...(agent_nonce === undefined ? {} : { agent_nonce }) });
|
|
108
|
-
this.options.on_hello?.(this);
|
|
109
|
-
}
|
|
110
|
-
// Handles one text frame from the server; anything that is not a message is ignored, unless the agent checks the server.
|
|
111
|
-
receive(text) {
|
|
112
|
-
if (this.closed)
|
|
113
|
-
return;
|
|
114
|
-
this.last_heard = Date.now();
|
|
115
|
-
if (!this.checks_server) {
|
|
116
|
-
this.dispatch(text);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (this.signed === null) {
|
|
120
|
-
this.receive_challenge(text);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const opened = this.open_signed(this.signed, text);
|
|
124
|
-
if (opened.error !== null)
|
|
125
|
-
this.refuse(opened.error, text);
|
|
126
|
-
else
|
|
127
|
-
this.dispatch(opened.text);
|
|
128
|
-
}
|
|
129
|
-
// The connection has gone: stops the streams that were being sent, since nobody reads them now.
|
|
130
|
-
close() {
|
|
131
|
-
if (this.closed)
|
|
132
|
-
return;
|
|
133
|
-
this.closed = true;
|
|
134
|
-
clearInterval(this.heartbeat);
|
|
135
|
-
clearTimeout(this.challenge_timer);
|
|
136
|
-
for (const pump of this.pumps.values())
|
|
137
|
-
pump.cancel();
|
|
138
|
-
this.pumps.clear();
|
|
139
|
-
}
|
|
140
|
-
// ── Sending ──────────────────────────────────────────────────────────────
|
|
141
|
-
send(message) {
|
|
142
|
-
if (!this.closed && this.socket.readyState === socket_open)
|
|
143
|
-
this.socket.send(encode_message(message, this.options.app));
|
|
144
|
-
}
|
|
145
|
-
send_chunk(id, data) {
|
|
146
|
-
if (!this.closed && this.socket.readyState === socket_open)
|
|
147
|
-
this.socket.send(encode_chunk(id, data));
|
|
148
|
-
}
|
|
149
|
-
end_stream(id, result) {
|
|
150
|
-
this.pumps.delete(id);
|
|
151
|
-
this.send({ type: "stream_end", id, exit_code: result.exit_code, stderr: result.stderr });
|
|
152
|
-
}
|
|
153
|
-
// ── Checking the server ──────────────────────────────────────────────────
|
|
154
|
-
// Checks the server's signature of its challenge before signing anything, then says hello with a nonce of its own.
|
|
155
|
-
receive_challenge(text) {
|
|
156
|
-
const message = parse_server_message(text, this.options.app);
|
|
157
|
-
if (message === null || message.type !== "challenge" || !is_server_message(message)) {
|
|
158
|
-
this.refuse("The server sent a message before its signed challenge.", text);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const challenge = message;
|
|
162
|
-
const { app, host, server_keys } = this.options;
|
|
163
|
-
if (typeof challenge.server_key !== "string" || typeof challenge.server_signature !== "string") {
|
|
164
|
-
this.refuse("The server did not sign its challenge, and this agent answers only a server that signs with one of its server keys.", null);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const server_key = server_keys.get(challenge.server_key);
|
|
168
|
-
if (server_key === undefined) {
|
|
169
|
-
this.refuse(`The server signed with the key ${clip(challenge.server_key)}, which is not one of this agent's server keys.`, null);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
if (!verify_text(server_key, server_challenge_text(app.name, host, challenge.nonce, challenge.server_key), challenge.server_signature)) {
|
|
173
|
-
this.refuse(`The server's challenge for ${host} is not signed by its key ${challenge.server_key}.`, null);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
clearTimeout(this.challenge_timer);
|
|
177
|
-
const agent_nonce = new_nonce();
|
|
178
|
-
this.signed = { server_nonce: challenge.nonce, agent_nonce, server_key, last_counter: 0 };
|
|
179
|
-
const key = this.options.key;
|
|
180
|
-
const signature = key ? sign_text(key, agent_hello_text(app.name, host, challenge.nonce, agent_nonce, challenge.server_key)) : undefined;
|
|
181
|
-
this.say_hello(signature, agent_nonce);
|
|
182
|
-
}
|
|
183
|
-
// The text of a signed message whose counter follows the last and whose signature checks out, or why it does not.
|
|
184
|
-
open_signed(signed, text) {
|
|
185
|
-
const envelope = parse_signed_message(text);
|
|
186
|
-
if (envelope === null)
|
|
187
|
-
return { text: null, error: "The server sent a message it did not sign." };
|
|
188
|
-
if (envelope.counter !== signed.last_counter + 1) {
|
|
189
|
-
return { text: null, error: `The server's message counter went from ${signed.last_counter} to ${envelope.counter}, so a message was replayed, reordered or dropped.` };
|
|
190
|
-
}
|
|
191
|
-
const signed_text = server_message_text(this.options.app.name, this.options.host, signed.server_nonce, signed.agent_nonce, envelope.counter, envelope.message);
|
|
192
|
-
if (!verify_text(signed.server_key, signed_text, envelope.signature))
|
|
193
|
-
return { text: null, error: "The server's message is not signed by its key for this connection." };
|
|
194
|
-
signed.last_counter = envelope.counter;
|
|
195
|
-
return { text: envelope.message, error: null };
|
|
196
|
-
}
|
|
197
|
-
// Answers a refused message that carries an id with a failure, logs why, and hangs up.
|
|
198
|
-
refuse(reason, text) {
|
|
199
|
-
if (this.closed)
|
|
200
|
-
return;
|
|
201
|
-
const id = text === null ? null : request_id_of(text);
|
|
202
|
-
if (id !== null)
|
|
203
|
-
this.send({ type: "failed", id, error: `The agent refused the request: ${reason}` });
|
|
204
|
-
(this.options.log ?? console.error)(`Refused the server: ${reason}`);
|
|
205
|
-
this.socket.close(server_refused_close_code, "The server's signature did not check out.");
|
|
206
|
-
this.close();
|
|
207
|
-
}
|
|
208
|
-
// ── Handling ─────────────────────────────────────────────────────────────
|
|
209
|
-
// Handles one message's text once it is known to come from the server.
|
|
210
|
-
dispatch(text) {
|
|
211
|
-
const message = parse_server_message(text, this.options.app);
|
|
212
|
-
if (message === null)
|
|
213
|
-
return;
|
|
214
|
-
if (is_server_message(message))
|
|
215
|
-
this.handle(message);
|
|
216
|
-
else
|
|
217
|
-
this.handle_app(message);
|
|
218
|
-
}
|
|
219
|
-
handle(message) {
|
|
220
|
-
switch (message.type) {
|
|
221
|
-
case "ping":
|
|
222
|
-
this.send({ type: "pong", epoch_ms: message.epoch_ms });
|
|
223
|
-
return;
|
|
224
|
-
case "pong":
|
|
225
|
-
return;
|
|
226
|
-
case "challenge":
|
|
227
|
-
if (this.options.key && !this.greeted)
|
|
228
|
-
this.say_hello(sign_challenge(this.options.key, this.options.app.name, this.options.host, message.nonce));
|
|
229
|
-
return;
|
|
230
|
-
case "credit":
|
|
231
|
-
this.pumps.get(message.id)?.grant(message.bytes);
|
|
232
|
-
return;
|
|
233
|
-
case "cancel":
|
|
234
|
-
this.pumps.get(message.id)?.cancel();
|
|
235
|
-
return;
|
|
236
|
-
case "stream":
|
|
237
|
-
this.start_pump(message.id, () => this.open_stream(message.task, message.stdin), message.window);
|
|
238
|
-
return;
|
|
239
|
-
case "attach":
|
|
240
|
-
this.start_pump(message.id, () => this.runs.tail(message.run_id, message.offset), message.window);
|
|
241
|
-
return;
|
|
242
|
-
default:
|
|
243
|
-
void this.answer(message.id, () => this.value_for(message));
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
// A message carrying an id is a request the app answers; anything else goes to the app as it is.
|
|
247
|
-
handle_app(message) {
|
|
248
|
-
if (typeof message.id !== "string") {
|
|
249
|
-
this.options.on_message?.(message, this);
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
const requests = this.options.requests ?? {};
|
|
253
|
-
const handler = Object.hasOwn(requests, message.type) ? requests[message.type] : undefined;
|
|
254
|
-
if (handler === undefined) {
|
|
255
|
-
this.send({ type: "failed", id: message.id, error: `This agent does not know the request '${message.type}'.` });
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
void this.answer(message.id, () => handler(message));
|
|
259
|
-
}
|
|
260
|
-
start_pump(id, open, window) {
|
|
261
|
-
if (this.pumps.has(id))
|
|
262
|
-
return;
|
|
263
|
-
let stream;
|
|
264
|
-
try {
|
|
265
|
-
stream = open();
|
|
266
|
-
}
|
|
267
|
-
catch (error) {
|
|
268
|
-
this.send({ type: "stream_end", id, exit_code: -1, stderr: error.message });
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
this.pumps.set(id, new OutputPump(id, stream, window, this));
|
|
272
|
-
}
|
|
273
|
-
async answer(id, work) {
|
|
274
|
-
try {
|
|
275
|
-
const value = await work();
|
|
276
|
-
this.send({ type: "reply", id, value: value ?? null });
|
|
277
|
-
}
|
|
278
|
-
catch (error) {
|
|
279
|
-
this.send({ type: "failed", id, error: error.message });
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
open_stream(task, stdin) {
|
|
283
|
-
if (this.options.runner.stream === undefined)
|
|
284
|
-
throw new Error("This agent does not stream its tasks.");
|
|
285
|
-
return this.options.runner.stream(task, { stdin });
|
|
286
|
-
}
|
|
287
|
-
async value_for(message) {
|
|
288
|
-
switch (message.type) {
|
|
289
|
-
case "run":
|
|
290
|
-
return this.options.runner.run(message.task, { timeout_in_seconds: message.timeout_in_seconds, stdin: message.stdin });
|
|
291
|
-
case "start":
|
|
292
|
-
return this.runs.start(message.run_id, message.task, message.stdin);
|
|
293
|
-
case "poll":
|
|
294
|
-
return this.runs.poll(message.run_id, message.offset);
|
|
295
|
-
case "stop":
|
|
296
|
-
return this.runs.stop(message.run_id);
|
|
297
|
-
case "forget":
|
|
298
|
-
this.runs.forget(message.run_id);
|
|
299
|
-
return null;
|
|
300
|
-
case "runs":
|
|
301
|
-
return this.runs.list();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
// Pings the server, or gives up on one that has been silent for three beats.
|
|
305
|
-
beat() {
|
|
306
|
-
if (Date.now() - this.last_heard > 3 * this.options.heartbeat_in_milliseconds) {
|
|
307
|
-
this.socket.close(4008, "The server stopped answering.");
|
|
308
|
-
this.close();
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
this.send({ type: "ping", epoch_ms: Date.now() });
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
315
|
-
// The id of the request a refused frame carries, read from the message a signed frame wraps, or null for none.
|
|
316
|
-
function request_id_of(text) {
|
|
317
|
-
const record_of = (value) => {
|
|
318
|
-
if (typeof value !== "string")
|
|
319
|
-
return {};
|
|
320
|
-
try {
|
|
321
|
-
const parsed = JSON.parse(value);
|
|
322
|
-
return typeof parsed === "object" && parsed !== null ? parsed : {};
|
|
323
|
-
}
|
|
324
|
-
catch {
|
|
325
|
-
return {};
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
const outer = record_of(text);
|
|
329
|
-
const id = typeof outer.message === "string" ? record_of(outer.message).id : outer.id;
|
|
330
|
-
return typeof id === "string" ? clip(id) : null;
|
|
331
|
-
}
|
|
332
|
-
// A text from the server cut short enough to log or echo back.
|
|
333
|
-
function clip(text) {
|
|
334
|
-
return text.length <= 128 ? text : `${text.slice(0, 128)}…`;
|
|
335
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/session";
|
|
@@ -1,12 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
host_name: string | null;
|
|
3
|
-
user: string | null;
|
|
4
|
-
port: number | null;
|
|
5
|
-
identity_files: string[];
|
|
6
|
-
}
|
|
7
|
-
export interface SshConfigContext {
|
|
8
|
-
home: string;
|
|
9
|
-
local_user: string;
|
|
10
|
-
}
|
|
11
|
-
export declare function host_line_matches(patterns: readonly string[], host: string): boolean;
|
|
12
|
-
export declare function ssh_host_options(text: string, host: string, context: SshConfigContext): SshHostOptions;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/ssh_config";
|
package/dist/agent/ssh_config.js
CHANGED
|
@@ -1,72 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// OpenSSH reads it, the first value given for each keyword wins, and a Host line's patterns take * and ? and a !
|
|
4
|
-
// pattern that rules a host out. Match blocks and Include are not followed.
|
|
5
|
-
// ── Patterns ─────────────────────────────────────────────────────────────────
|
|
6
|
-
function pattern_matches(pattern, host) {
|
|
7
|
-
const expression = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("*", ".*").replaceAll("?", ".");
|
|
8
|
-
return new RegExp(`^${expression}$`, "i").test(host);
|
|
9
|
-
}
|
|
10
|
-
// Whether a Host line's patterns take the host: one matches and no ! pattern rules it out.
|
|
11
|
-
export function host_line_matches(patterns, host) {
|
|
12
|
-
let matched = false;
|
|
13
|
-
for (const pattern of patterns) {
|
|
14
|
-
if (pattern.startsWith("!")) {
|
|
15
|
-
if (pattern_matches(pattern.slice(1), host))
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
else if (pattern_matches(pattern, host))
|
|
19
|
-
matched = true;
|
|
20
|
-
}
|
|
21
|
-
return matched;
|
|
22
|
-
}
|
|
23
|
-
// ── Reading ──────────────────────────────────────────────────────────────────
|
|
24
|
-
// A line's keyword and its arguments, quotes kept together, or null for a blank line or a comment.
|
|
25
|
-
function split_line(line) {
|
|
26
|
-
const text = line.trim();
|
|
27
|
-
if (text === "" || text.startsWith("#"))
|
|
28
|
-
return null;
|
|
29
|
-
const words = [...text.matchAll(/"([^"]*)"|(\S+)/g)].map((match) => match[1] ?? match[2]);
|
|
30
|
-
const [first = "", ...rest] = words;
|
|
31
|
-
// "Keyword=value" and "Keyword = value" both name a value.
|
|
32
|
-
const equals = first.indexOf("=");
|
|
33
|
-
if (equals > 0)
|
|
34
|
-
return [first.slice(0, equals).toLowerCase(), [first.slice(equals + 1), ...rest].filter((word) => word !== "")];
|
|
35
|
-
return [first.toLowerCase(), rest.filter((word, index) => !(index === 0 && word === "="))];
|
|
36
|
-
}
|
|
37
|
-
function expand(path, host, context) {
|
|
38
|
-
const filled = path.replace(/%([%hu])/g, (_whole, code) => (code === "%" ? "%" : code === "h" ? host : context.local_user));
|
|
39
|
-
return filled === "~" ? context.home : filled.startsWith("~/") ? `${context.home}/${filled.slice(2)}` : filled;
|
|
40
|
-
}
|
|
41
|
-
// What the config's text says of the host given, the first value of each keyword winning.
|
|
42
|
-
export function ssh_host_options(text, host, context) {
|
|
43
|
-
const options = { host_name: null, user: null, port: null, identity_files: [] };
|
|
44
|
-
// Lines before any Host or Match line apply to every host.
|
|
45
|
-
let applies = true;
|
|
46
|
-
for (const line of text.split(/\r?\n/)) {
|
|
47
|
-
const split = split_line(line);
|
|
48
|
-
if (split === null)
|
|
49
|
-
continue;
|
|
50
|
-
const [keyword, args] = split;
|
|
51
|
-
if (keyword === "host") {
|
|
52
|
-
applies = host_line_matches(args, host);
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
if (keyword === "match") {
|
|
56
|
-
applies = false;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
if (!applies || args.length === 0)
|
|
60
|
-
continue;
|
|
61
|
-
const value = args[0];
|
|
62
|
-
if (keyword === "hostname" && options.host_name === null)
|
|
63
|
-
options.host_name = value.replaceAll("%h", host);
|
|
64
|
-
else if (keyword === "user" && options.user === null)
|
|
65
|
-
options.user = value;
|
|
66
|
-
else if (keyword === "port" && options.port === null && /^\d+$/.test(value))
|
|
67
|
-
options.port = Number(value);
|
|
68
|
-
else if (keyword === "identityfile" && value.toLowerCase() !== "none")
|
|
69
|
-
options.identity_files.push(expand(value, options.host_name ?? host, context));
|
|
70
|
-
}
|
|
71
|
-
return options;
|
|
72
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/ssh_config";
|
package/dist/agent/stalls.d.ts
CHANGED
|
@@ -1,44 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare const later_notes_in_seconds = 1800;
|
|
3
|
-
export interface ProcessInfo {
|
|
4
|
-
pid: number;
|
|
5
|
-
ppid: number;
|
|
6
|
-
name: string;
|
|
7
|
-
age_in_seconds: number;
|
|
8
|
-
}
|
|
9
|
-
export interface LockEntry {
|
|
10
|
-
pid: number;
|
|
11
|
-
waiting: boolean;
|
|
12
|
-
mode: "READ" | "WRITE";
|
|
13
|
-
file: string;
|
|
14
|
-
}
|
|
15
|
-
export interface KnownLock {
|
|
16
|
-
name: string;
|
|
17
|
-
hint?: string;
|
|
18
|
-
}
|
|
19
|
-
export interface StallOptions {
|
|
20
|
-
proc?: string;
|
|
21
|
-
known_locks?: Record<string, KnownLock>;
|
|
22
|
-
lock_files?: Record<string, string>;
|
|
23
|
-
mark?: string;
|
|
24
|
-
}
|
|
25
|
-
export declare function read_processes(proc?: string): Map<number, ProcessInfo>;
|
|
26
|
-
export declare function read_locks(proc?: string): LockEntry[];
|
|
27
|
-
export declare function lock_file_id(path: string): string | null;
|
|
28
|
-
export declare function lock_file_ids(paths: readonly string[]): Record<string, string>;
|
|
29
|
-
export declare function descendants(processes: Map<number, ProcessInfo>, root: number): ProcessInfo[];
|
|
30
|
-
export declare function lock_holders(path: string, except?: Set<number>, proc?: string, file_id?: string | null): ProcessInfo[];
|
|
31
|
-
export declare function show_process(info: ProcessInfo): string;
|
|
32
|
-
export declare function show_holders(holders: ProcessInfo[]): string;
|
|
33
|
-
export declare function describe_stall(root: number, quiet_in_seconds: number, options?: StallOptions): string[];
|
|
34
|
-
export interface StallWatch {
|
|
35
|
-
heard(): void;
|
|
36
|
-
look(): void;
|
|
37
|
-
stop(): void;
|
|
38
|
-
}
|
|
39
|
-
export interface StallWatchOptions extends StallOptions {
|
|
40
|
-
now?: () => number;
|
|
41
|
-
first_note_in_seconds?: number;
|
|
42
|
-
later_notes_in_seconds?: number;
|
|
43
|
-
}
|
|
44
|
-
export declare function watch_for_stalls(root: number, write: (text: string) => void, options?: StallWatchOptions): StallWatch;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/stalls";
|