@agentchatme/agent-core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -36
- package/dist/{chunk-I4U4A7HO.js → chunk-GXAX47O6.js} +385 -148
- package/dist/chunk-GXAX47O6.js.map +1 -0
- package/dist/daemon-entry.d.ts +165 -140
- package/dist/daemon-entry.js +534 -442
- package/dist/daemon-entry.js.map +1 -1
- package/dist/index.d.ts +62 -80
- package/dist/index.js +186 -184
- package/dist/index.js.map +1 -1
- package/dist/prompts-B0UL03Hu.d.ts +336 -0
- package/package.json +3 -3
- package/dist/chunk-I4U4A7HO.js.map +0 -1
- package/dist/status-D8qOP4kM.d.ts +0 -260
package/dist/daemon-entry.js
CHANGED
|
@@ -1,30 +1,38 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
TextsStore,
|
|
3
3
|
acquireLeaderLock,
|
|
4
|
+
atomicWriteFile,
|
|
4
5
|
beat,
|
|
6
|
+
buildRunPrompts,
|
|
7
|
+
clientIdentityOf,
|
|
5
8
|
credentialsPath,
|
|
6
|
-
|
|
7
|
-
getMeLite,
|
|
9
|
+
external_exports,
|
|
8
10
|
idle,
|
|
9
|
-
|
|
11
|
+
integrationDisabledBy,
|
|
10
12
|
log,
|
|
13
|
+
normalizeHandle,
|
|
14
|
+
readJsonFile,
|
|
11
15
|
resolveIdentity,
|
|
12
16
|
resolveRunBudgetMs,
|
|
13
17
|
resolveRunProfile,
|
|
14
18
|
spawnCommand,
|
|
15
19
|
spawnCommandSync,
|
|
16
20
|
trayHasWaiting,
|
|
17
|
-
|
|
18
|
-
} from "./chunk-
|
|
21
|
+
writeIntegrationStatus
|
|
22
|
+
} from "./chunk-GXAX47O6.js";
|
|
19
23
|
|
|
20
|
-
// src/
|
|
21
|
-
import * as
|
|
24
|
+
// src/integration/run.ts
|
|
25
|
+
import * as path3 from "path";
|
|
22
26
|
import * as fs from "fs";
|
|
23
27
|
|
|
24
28
|
// src/daemon/config.ts
|
|
25
29
|
import * as path from "path";
|
|
30
|
+
import { AgentChatClient } from "agentchatme";
|
|
31
|
+
function wsBaseFor(apiBase) {
|
|
32
|
+
return apiBase.replace(/^http/, "ws").replace(/\/+$/, "");
|
|
33
|
+
}
|
|
26
34
|
function wsUrlFor(apiBase) {
|
|
27
|
-
return apiBase
|
|
35
|
+
return `${wsBaseFor(apiBase)}/v1/ws`;
|
|
28
36
|
}
|
|
29
37
|
async function resolveDaemonConfig(opts) {
|
|
30
38
|
const home = path.resolve(opts.home);
|
|
@@ -34,8 +42,14 @@ async function resolveDaemonConfig(opts) {
|
|
|
34
42
|
}
|
|
35
43
|
let handle = id.source === "file" ? id.handle : null;
|
|
36
44
|
if (handle === null) {
|
|
37
|
-
const
|
|
38
|
-
|
|
45
|
+
const client = new AgentChatClient({
|
|
46
|
+
apiKey: id.apiKey,
|
|
47
|
+
baseUrl: id.apiBase,
|
|
48
|
+
clientIdentity: clientIdentityOf(opts.client),
|
|
49
|
+
timeoutMs: 1e4
|
|
50
|
+
});
|
|
51
|
+
const me = await client.getMe().catch(() => null);
|
|
52
|
+
if (me === null || typeof me.handle !== "string" || me.handle.length === 0) {
|
|
39
53
|
throw new Error(
|
|
40
54
|
"could not determine this agent\u2019s handle (no credentials file, and /v1/agents/me did not answer)"
|
|
41
55
|
);
|
|
@@ -46,18 +60,212 @@ async function resolveDaemonConfig(opts) {
|
|
|
46
60
|
apiKey: id.apiKey,
|
|
47
61
|
handle,
|
|
48
62
|
apiBase: id.apiBase,
|
|
49
|
-
|
|
63
|
+
wsBase: wsBaseFor(id.apiBase),
|
|
50
64
|
home,
|
|
51
|
-
workdir:
|
|
65
|
+
workdir: path.resolve(opts.workdir)
|
|
52
66
|
};
|
|
53
67
|
}
|
|
54
68
|
|
|
55
|
-
// src/
|
|
69
|
+
// src/integration/integration.ts
|
|
70
|
+
import { AgentChatClient as AgentChatClient2 } from "agentchatme";
|
|
71
|
+
|
|
72
|
+
// src/integration/connection.ts
|
|
73
|
+
import { EventEmitter } from "events";
|
|
74
|
+
import { RealtimeClient } from "agentchatme";
|
|
75
|
+
function socketStateOf(next, previous) {
|
|
76
|
+
switch (next) {
|
|
77
|
+
case "idle":
|
|
78
|
+
return "idle";
|
|
79
|
+
case "connecting":
|
|
80
|
+
if (previous === "blocked") return "blocked";
|
|
81
|
+
return previous === "idle" || previous === "connecting" ? "connecting" : "reconnecting";
|
|
82
|
+
case "ready":
|
|
83
|
+
return "ready";
|
|
84
|
+
case "backoff":
|
|
85
|
+
return "reconnecting";
|
|
86
|
+
case "blocked":
|
|
87
|
+
return "blocked";
|
|
88
|
+
case "stopped":
|
|
89
|
+
return "closed";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function blockedDetail(info) {
|
|
93
|
+
return `auth rejected (close ${info.code}${info.reason ? ` ${info.reason}` : ""}) \u2014 check the agent's API key; a fresh hello is tried every 10\u201315 min until one is accepted`;
|
|
94
|
+
}
|
|
95
|
+
function stringField(record, keys) {
|
|
96
|
+
for (const key of keys) {
|
|
97
|
+
const value = record[key];
|
|
98
|
+
if (typeof value === "string" && value.length > 0) return value;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
function asRecord(value) {
|
|
103
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
104
|
+
}
|
|
105
|
+
var IntegrationConnection = class extends EventEmitter {
|
|
106
|
+
constructor(options) {
|
|
107
|
+
super();
|
|
108
|
+
this.options = options;
|
|
109
|
+
this.selfHandle = normalizeHandle(options.selfHandle);
|
|
110
|
+
}
|
|
111
|
+
options;
|
|
112
|
+
rt = null;
|
|
113
|
+
_state = "idle";
|
|
114
|
+
stopped = false;
|
|
115
|
+
/** Rejected hellos since the last accepted one: the first is an error line, the rest are info. */
|
|
116
|
+
refusals = 0;
|
|
117
|
+
selfHandle;
|
|
118
|
+
get state() {
|
|
119
|
+
return this._state;
|
|
120
|
+
}
|
|
121
|
+
/** True only while the socket is live — `hello.ok` received, not merely open. */
|
|
122
|
+
get connected() {
|
|
123
|
+
return this._state === "ready";
|
|
124
|
+
}
|
|
125
|
+
on(event, listener) {
|
|
126
|
+
return super.on(event, listener);
|
|
127
|
+
}
|
|
128
|
+
/** Open the SDK's client. Once: it lives until `stop()`, reconnecting on its own. */
|
|
129
|
+
start() {
|
|
130
|
+
if (this.rt !== null || this.stopped) return;
|
|
131
|
+
const rt = new RealtimeClient({
|
|
132
|
+
apiKey: this.options.apiKey,
|
|
133
|
+
baseUrl: this.options.wsBase,
|
|
134
|
+
clientIdentity: clientIdentityOf(this.options.client),
|
|
135
|
+
// Delivered = this machine received it. Acked at parse time, before
|
|
136
|
+
// anything here runs; read state is the agent's verdict, separately.
|
|
137
|
+
ackOnReceipt: true,
|
|
138
|
+
reconnect: true,
|
|
139
|
+
...this.options.reconnectIntervalMs !== void 0 ? { reconnectInterval: this.options.reconnectIntervalMs } : {},
|
|
140
|
+
...this.options.maxReconnectIntervalMs !== void 0 ? { maxReconnectInterval: this.options.maxReconnectIntervalMs } : {}
|
|
141
|
+
});
|
|
142
|
+
this.rt = rt;
|
|
143
|
+
this.setState("connecting");
|
|
144
|
+
rt.on("state", (next) => this.setState(socketStateOf(next, this._state)));
|
|
145
|
+
rt.on("blocked", (info) => this.onBlocked(info));
|
|
146
|
+
rt.onConnect(() => {
|
|
147
|
+
this.refusals = 0;
|
|
148
|
+
log.info(`socket ready (hello.ok) \u2014 acks ${rt.ackNegotiated ? "on receipt" : "not negotiated by this server"}`);
|
|
149
|
+
this.emit("ready");
|
|
150
|
+
});
|
|
151
|
+
rt.onDisconnect(({ code, reason, initiator }) => {
|
|
152
|
+
if (this.stopped) return;
|
|
153
|
+
log.warn(`socket closed (${code}${reason ? ` ${reason}` : ""}${initiator === "client" ? ", by this client" : ""})`);
|
|
154
|
+
});
|
|
155
|
+
rt.onError((err) => {
|
|
156
|
+
if (this.stopped) return;
|
|
157
|
+
log.warn(`socket: ${err.message}`);
|
|
158
|
+
});
|
|
159
|
+
rt.on("message.new", (message) => {
|
|
160
|
+
if (!this.stopped) this.onMessageFrame(message);
|
|
161
|
+
});
|
|
162
|
+
rt.on("wake", (frame) => {
|
|
163
|
+
if (!this.stopped) this.onWakeFrame(frame);
|
|
164
|
+
});
|
|
165
|
+
rt.on("group.invite.received", (message) => {
|
|
166
|
+
if (!this.stopped) this.onInviteFrame(message);
|
|
167
|
+
});
|
|
168
|
+
rt.connect().catch((err) => {
|
|
169
|
+
if (this.stopped || rt.state !== "idle") return;
|
|
170
|
+
log.error(`socket cannot be opened (${err instanceof Error ? err.message : String(err)}) \u2014 no WebSocket implementation; holding no socket`);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** End the SDK's client. Final: a stopped connection is not restarted. */
|
|
174
|
+
stop() {
|
|
175
|
+
if (this.stopped) return;
|
|
176
|
+
this.stopped = true;
|
|
177
|
+
const rt = this.rt;
|
|
178
|
+
this.rt = null;
|
|
179
|
+
try {
|
|
180
|
+
rt?.disconnect();
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
this.setState("closed");
|
|
184
|
+
}
|
|
185
|
+
setState(next) {
|
|
186
|
+
const prev = this._state;
|
|
187
|
+
if (prev === next) return;
|
|
188
|
+
this._state = next;
|
|
189
|
+
if (next === "connecting") log.info("socket connecting");
|
|
190
|
+
else if (next === "reconnecting") log.info("socket reconnecting \u2014 the SDK backs off and retries; it never gives up");
|
|
191
|
+
else if (next === "closed") log.info("socket closed");
|
|
192
|
+
this.emit("state", next, prev);
|
|
193
|
+
}
|
|
194
|
+
/** The SDK is `blocked`: the server refused the credential on the hello. Fires per rejected attempt. */
|
|
195
|
+
onBlocked(info) {
|
|
196
|
+
if (this.stopped) return;
|
|
197
|
+
const detail = blockedDetail(info);
|
|
198
|
+
this.refusals += 1;
|
|
199
|
+
if (this.refusals === 1) {
|
|
200
|
+
log.error(`socket blocked (auth): ${detail}`);
|
|
201
|
+
} else {
|
|
202
|
+
const next = info.retryInMs === null ? "no retry" : `next hello in about ${Math.max(1, Math.round(info.retryInMs / 6e4))} min`;
|
|
203
|
+
log.info(`socket still blocked (auth): close ${info.code}${info.reason ? ` ${info.reason}` : ""} \u2014 ${next}`);
|
|
204
|
+
}
|
|
205
|
+
this.emit("blocked", detail, info);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* A `message.new` frame. Only the sender (echo filter) and the ids (log
|
|
209
|
+
* line) are read. `content`, `metadata`, previews — never touched. The SDK
|
|
210
|
+
* acked it before this ran.
|
|
211
|
+
*/
|
|
212
|
+
onMessageFrame(message) {
|
|
213
|
+
const payload = asRecord(message.payload);
|
|
214
|
+
if (payload === null) {
|
|
215
|
+
log.warn("socket: message.new without an object payload \u2014 dropped");
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const senderRaw = stringField(payload, ["sender", "sender_handle", "from"]);
|
|
219
|
+
const sender = senderRaw === null ? null : normalizeHandle(senderRaw);
|
|
220
|
+
const messageId = stringField(payload, ["id"]);
|
|
221
|
+
const conversationId = stringField(payload, ["conversation_id"]);
|
|
222
|
+
if (sender !== null && sender === this.selfHandle) {
|
|
223
|
+
log.debug(`socket: own echo in ${conversationId ?? "?"} \u2014 not a wake`);
|
|
224
|
+
this.emit("echo");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
log.info(`wake \u2014 message.new conv=${conversationId ?? "?"} msg=${messageId ?? "?"} from=@${sender ?? "?"}`);
|
|
228
|
+
this.emit("wake", { source: "message", sender, conversationId, messageId });
|
|
229
|
+
}
|
|
230
|
+
/** A `wake` frame, validated by the SDK. No message, nothing to ack. */
|
|
231
|
+
onWakeFrame(frame) {
|
|
232
|
+
const { conversation_id, anchor_message_id, step, reason } = frame.payload;
|
|
233
|
+
log.info(`wake \u2014 ${reason} conv=${conversation_id} anchor=${anchor_message_id} step=${step}`);
|
|
234
|
+
this.emit("wake", {
|
|
235
|
+
source: "reminder",
|
|
236
|
+
conversationId: conversation_id,
|
|
237
|
+
anchorMessageId: anchor_message_id,
|
|
238
|
+
step
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
onInviteFrame(message) {
|
|
242
|
+
const payload = asRecord(message.payload);
|
|
243
|
+
if (payload === null) {
|
|
244
|
+
log.warn("socket: group.invite.received without an object payload \u2014 dropped");
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const inviteId = stringField(payload, ["invite_id", "id"]);
|
|
248
|
+
if (inviteId === null) {
|
|
249
|
+
log.warn("socket: group invite without an invite id \u2014 dropped");
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const invite = {
|
|
253
|
+
inviteId,
|
|
254
|
+
groupId: stringField(payload, ["group_id", "conversation_id"]),
|
|
255
|
+
groupName: stringField(payload, ["group_name", "name"]),
|
|
256
|
+
inviterHandle: stringField(payload, ["inviter_handle", "inviter", "from"])
|
|
257
|
+
};
|
|
258
|
+
log.info(`wake \u2014 group invite ${invite.inviteId} to ${invite.groupId ?? "?"} from=@${invite.inviterHandle ?? "?"}`);
|
|
259
|
+
this.emit("invite", invite);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/integration/invoker.ts
|
|
56
264
|
import * as crypto from "crypto";
|
|
57
265
|
|
|
58
|
-
// src/
|
|
59
|
-
var
|
|
60
|
-
var
|
|
266
|
+
// src/integration/wake.ts
|
|
267
|
+
var DEFAULT_WAKE_DEBOUNCE_MS = 2e3;
|
|
268
|
+
var WakeCoalescer = class {
|
|
61
269
|
onFire;
|
|
62
270
|
debounceMs;
|
|
63
271
|
now;
|
|
@@ -67,11 +275,11 @@ var KnockCoalescer = class {
|
|
|
67
275
|
delayed = null;
|
|
68
276
|
delayedDeadline = null;
|
|
69
277
|
stopped = false;
|
|
70
|
-
|
|
278
|
+
wakes = 0;
|
|
71
279
|
absorbed = 0;
|
|
72
280
|
fires = 0;
|
|
73
281
|
constructor(options) {
|
|
74
|
-
const debounce = options.debounceMs ??
|
|
282
|
+
const debounce = options.debounceMs ?? DEFAULT_WAKE_DEBOUNCE_MS;
|
|
75
283
|
if (!(debounce >= 0)) throw new RangeError("debounceMs must be >= 0");
|
|
76
284
|
this.onFire = options.onFire;
|
|
77
285
|
this.debounceMs = debounce;
|
|
@@ -83,26 +291,26 @@ var KnockCoalescer = class {
|
|
|
83
291
|
get armed() {
|
|
84
292
|
return this.timer !== null;
|
|
85
293
|
}
|
|
86
|
-
/** True while a retry
|
|
294
|
+
/** True while a retry wake is scheduled and has not fired. */
|
|
87
295
|
get delayedPending() {
|
|
88
296
|
return this.delayed !== null;
|
|
89
297
|
}
|
|
90
|
-
/** Epoch ms of the pending retry
|
|
298
|
+
/** Epoch ms of the pending retry wake, or null. */
|
|
91
299
|
get delayedDue() {
|
|
92
300
|
return this.delayedDeadline;
|
|
93
301
|
}
|
|
94
|
-
/** A
|
|
95
|
-
|
|
302
|
+
/** A wake: arm the timer if none is armed; otherwise absorb. */
|
|
303
|
+
wake() {
|
|
96
304
|
if (this.stopped) return;
|
|
97
|
-
this.
|
|
305
|
+
this.wakes += 1;
|
|
98
306
|
if (this.timer !== null) {
|
|
99
307
|
this.absorbed += 1;
|
|
100
308
|
return;
|
|
101
309
|
}
|
|
102
310
|
this.timer = this._setTimeout(() => this.fire(), this.debounceMs);
|
|
103
311
|
}
|
|
104
|
-
/** A delayed
|
|
105
|
-
|
|
312
|
+
/** A delayed wake (retry ladder). The sooner pending deadline is kept. */
|
|
313
|
+
wakeAfter(delayMs) {
|
|
106
314
|
if (this.stopped) return;
|
|
107
315
|
const delay = Math.max(0, delayMs);
|
|
108
316
|
const deadline = this.now() + delay;
|
|
@@ -116,12 +324,12 @@ var KnockCoalescer = class {
|
|
|
116
324
|
this.delayed = this._setTimeout(() => {
|
|
117
325
|
this.delayed = null;
|
|
118
326
|
this.delayedDeadline = null;
|
|
119
|
-
this.
|
|
327
|
+
this.wake();
|
|
120
328
|
}, delay);
|
|
121
329
|
}
|
|
122
330
|
/**
|
|
123
|
-
* Drop a pending retry
|
|
124
|
-
* The debounce timer and later
|
|
331
|
+
* Drop a pending retry wake, if there is one; returns whether there was.
|
|
332
|
+
* The debounce timer and later wakes are unaffected. For when nothing is
|
|
125
333
|
* owed any more — a run read the whole tray, or the tray was found empty —
|
|
126
334
|
* so the retry would only ring for a backlog that is already handled.
|
|
127
335
|
*/
|
|
@@ -132,7 +340,7 @@ var KnockCoalescer = class {
|
|
|
132
340
|
this.delayedDeadline = null;
|
|
133
341
|
return true;
|
|
134
342
|
}
|
|
135
|
-
/** Cancel everything; later
|
|
343
|
+
/** Cancel everything; later wakes are ignored. */
|
|
136
344
|
stop() {
|
|
137
345
|
this.stopped = true;
|
|
138
346
|
if (this.timer !== null) {
|
|
@@ -152,11 +360,70 @@ var KnockCoalescer = class {
|
|
|
152
360
|
}
|
|
153
361
|
};
|
|
154
362
|
|
|
155
|
-
// src/
|
|
363
|
+
// src/integration/invites.ts
|
|
364
|
+
import * as path2 from "path";
|
|
365
|
+
var OFFERED_INVITES_FILE = "invites.offered.json";
|
|
366
|
+
var MAX_REMEMBERED = 1e3;
|
|
367
|
+
var Schema = external_exports.object({
|
|
368
|
+
version: external_exports.literal(1),
|
|
369
|
+
offered: external_exports.array(external_exports.object({ id: external_exports.string(), at: external_exports.string() }))
|
|
370
|
+
});
|
|
371
|
+
function offeredInvitesPath(home) {
|
|
372
|
+
return path2.join(home, OFFERED_INVITES_FILE);
|
|
373
|
+
}
|
|
374
|
+
var OfferedInvites = class _OfferedInvites {
|
|
375
|
+
/** `home === null` keeps the set in memory only (tests). */
|
|
376
|
+
constructor(home, entries = [], now = () => /* @__PURE__ */ new Date()) {
|
|
377
|
+
this.home = home;
|
|
378
|
+
this.now = now;
|
|
379
|
+
this.entries = entries.slice(-MAX_REMEMBERED);
|
|
380
|
+
}
|
|
381
|
+
home;
|
|
382
|
+
now;
|
|
383
|
+
entries;
|
|
384
|
+
/** Read the set from the identity home; a missing or malformed file is an empty set. */
|
|
385
|
+
static load(home, now) {
|
|
386
|
+
const parsed = Schema.safeParse(readJsonFile(offeredInvitesPath(home)));
|
|
387
|
+
return new _OfferedInvites(home, parsed.success ? parsed.data.offered : [], now);
|
|
388
|
+
}
|
|
389
|
+
get size() {
|
|
390
|
+
return this.entries.length;
|
|
391
|
+
}
|
|
392
|
+
has(inviteId) {
|
|
393
|
+
return this.entries.some((entry) => entry.id === inviteId);
|
|
394
|
+
}
|
|
395
|
+
add(inviteId) {
|
|
396
|
+
if (this.has(inviteId)) return;
|
|
397
|
+
this.entries.push({ id: inviteId, at: this.now().toISOString() });
|
|
398
|
+
while (this.entries.length > MAX_REMEMBERED) this.entries.shift();
|
|
399
|
+
this.persist();
|
|
400
|
+
}
|
|
401
|
+
remove(inviteId) {
|
|
402
|
+
const index = this.entries.findIndex((entry) => entry.id === inviteId);
|
|
403
|
+
if (index === -1) return;
|
|
404
|
+
this.entries.splice(index, 1);
|
|
405
|
+
this.persist();
|
|
406
|
+
}
|
|
407
|
+
persist() {
|
|
408
|
+
if (this.home === null) return;
|
|
409
|
+
try {
|
|
410
|
+
atomicWriteFile(
|
|
411
|
+
offeredInvitesPath(this.home),
|
|
412
|
+
`${JSON.stringify({ version: 1, offered: this.entries }, null, 2)}
|
|
413
|
+
`,
|
|
414
|
+
384
|
|
415
|
+
);
|
|
416
|
+
} catch (err) {
|
|
417
|
+
log.debug(`offered-invites write failed: ${String(err)}`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// src/integration/invoker.ts
|
|
156
423
|
var RETRY_LADDER_MS = [6e4, 3e5, 18e5];
|
|
157
424
|
var PREFLIGHT_WATCH_MS = 5 * 6e4;
|
|
158
|
-
var
|
|
159
|
-
var
|
|
425
|
+
var MAX_TAKEN = 1e3;
|
|
426
|
+
var IntegrationInvoker = class {
|
|
160
427
|
runner;
|
|
161
428
|
prompts;
|
|
162
429
|
budgetMs;
|
|
@@ -168,13 +435,23 @@ var DoorbellInvoker = class {
|
|
|
168
435
|
onChange;
|
|
169
436
|
newRunId;
|
|
170
437
|
trayHasWaiting;
|
|
438
|
+
offered;
|
|
171
439
|
coalescer;
|
|
172
440
|
cycleDue = false;
|
|
173
441
|
running = null;
|
|
174
442
|
inFlight = null;
|
|
175
443
|
stopped = false;
|
|
444
|
+
/** Invites queued for a run, in arrival order. */
|
|
176
445
|
invites = [];
|
|
177
|
-
|
|
446
|
+
/** Invites whose run did not start; offered again on the next wake. */
|
|
447
|
+
invitesAwaitingRetry = [];
|
|
448
|
+
/**
|
|
449
|
+
* Invite ids this process has taken, offered or not. The on-disk store says
|
|
450
|
+
* what was OFFERED; this says what is already in hand here — queued, in
|
|
451
|
+
* flight, or awaiting its retry — so the connect-time listing and a live
|
|
452
|
+
* frame for the same invite cannot queue it twice.
|
|
453
|
+
*/
|
|
454
|
+
taken = /* @__PURE__ */ new Set();
|
|
178
455
|
retryRung = 0;
|
|
179
456
|
preflightWatch = null;
|
|
180
457
|
preflightWasDown = false;
|
|
@@ -198,31 +475,36 @@ var DoorbellInvoker = class {
|
|
|
198
475
|
this.onChange = options.onChange ?? (() => void 0);
|
|
199
476
|
this.newRunId = options.runId ?? (() => crypto.randomUUID());
|
|
200
477
|
this.trayHasWaiting = options.trayHasWaiting ?? null;
|
|
201
|
-
this.
|
|
202
|
-
|
|
203
|
-
|
|
478
|
+
this.offered = options.offeredInvites ?? new OfferedInvites(null);
|
|
479
|
+
this.coalescer = new WakeCoalescer({
|
|
480
|
+
onFire: () => this.onWakeFired(),
|
|
481
|
+
debounceMs: options.debounceMs ?? DEFAULT_WAKE_DEBOUNCE_MS,
|
|
204
482
|
now: this.now,
|
|
205
483
|
...options.setTimeout ? { setTimeout: options.setTimeout } : {},
|
|
206
484
|
...options.clearTimeout ? { clearTimeout: options.clearTimeout } : {}
|
|
207
485
|
});
|
|
208
486
|
}
|
|
209
|
-
// ───
|
|
210
|
-
/** A
|
|
211
|
-
|
|
487
|
+
// ─── wakes ──────────────────────────────────────────────────────────────
|
|
488
|
+
/** A wake from the socket (a frame), the connect-time catch-up, or any other source. Coalesced, then run. */
|
|
489
|
+
wake(source) {
|
|
212
490
|
if (this.stopped) return;
|
|
213
|
-
log.debug(`
|
|
214
|
-
this.coalescer.
|
|
491
|
+
log.debug(`wake (${source})`);
|
|
492
|
+
this.coalescer.wake();
|
|
215
493
|
this.onChange();
|
|
216
494
|
}
|
|
217
495
|
/**
|
|
218
|
-
* The second
|
|
219
|
-
*
|
|
496
|
+
* The second wake kind. Offered once per machine: an invite already offered
|
|
497
|
+
* to a run, already queued, or awaiting its retry is not queued again.
|
|
498
|
+
* Returns true when newly queued.
|
|
220
499
|
*/
|
|
221
500
|
invite(invite) {
|
|
222
501
|
if (this.stopped) return false;
|
|
223
|
-
if (this.
|
|
224
|
-
this.
|
|
225
|
-
if (this.
|
|
502
|
+
if (this.offered.has(invite.inviteId) || this.taken.has(invite.inviteId)) return false;
|
|
503
|
+
this.taken.add(invite.inviteId);
|
|
504
|
+
if (this.taken.size > MAX_TAKEN) {
|
|
505
|
+
const oldest = this.taken.values().next().value;
|
|
506
|
+
if (oldest !== void 0) this.taken.delete(oldest);
|
|
507
|
+
}
|
|
226
508
|
this.invitesSeen += 1;
|
|
227
509
|
this.invites.push(invite);
|
|
228
510
|
this.onChange();
|
|
@@ -246,7 +528,7 @@ var DoorbellInvoker = class {
|
|
|
246
528
|
}
|
|
247
529
|
counters() {
|
|
248
530
|
return {
|
|
249
|
-
|
|
531
|
+
wakes: this.coalescer.wakes,
|
|
250
532
|
absorbed: this.coalescer.absorbed,
|
|
251
533
|
invites: this.invitesSeen,
|
|
252
534
|
runsStarted: this.runsStarted,
|
|
@@ -262,8 +544,13 @@ var DoorbellInvoker = class {
|
|
|
262
544
|
};
|
|
263
545
|
}
|
|
264
546
|
// ─── worker ─────────────────────────────────────────────────────────────
|
|
265
|
-
|
|
547
|
+
onWakeFired() {
|
|
266
548
|
this.cycleDue = true;
|
|
549
|
+
if (this.invitesAwaitingRetry.length > 0) {
|
|
550
|
+
const retry = this.invitesAwaitingRetry.splice(0);
|
|
551
|
+
log.info(`${retry.length} group invite(s) whose run did not start are offered again`);
|
|
552
|
+
this.invites.push(...retry);
|
|
553
|
+
}
|
|
267
554
|
this.schedule();
|
|
268
555
|
}
|
|
269
556
|
/** Run the worker if idle. One run at a time; work that lands mid-run runs afterwards. */
|
|
@@ -279,20 +566,20 @@ var DoorbellInvoker = class {
|
|
|
279
566
|
while (!this.stopped) {
|
|
280
567
|
const invite = this.invites.shift();
|
|
281
568
|
if (invite) {
|
|
282
|
-
await this.run("invite",
|
|
569
|
+
await this.run("invite", invite);
|
|
283
570
|
continue;
|
|
284
571
|
}
|
|
285
572
|
if (this.cycleDue) {
|
|
286
573
|
this.cycleDue = false;
|
|
287
574
|
if (!await this.trayOwesRun()) continue;
|
|
288
|
-
await this.run("
|
|
575
|
+
await this.run("wake");
|
|
289
576
|
continue;
|
|
290
577
|
}
|
|
291
578
|
return;
|
|
292
579
|
}
|
|
293
580
|
}
|
|
294
581
|
/**
|
|
295
|
-
* The count before a
|
|
582
|
+
* The count before a wake run. True: start the run (something waits, or
|
|
296
583
|
* the count could not be read). False: the tray is empty — no run, nothing
|
|
297
584
|
* owed — or the invoker stopped while counting.
|
|
298
585
|
*/
|
|
@@ -311,22 +598,22 @@ var DoorbellInvoker = class {
|
|
|
311
598
|
if (this.stopped) return false;
|
|
312
599
|
if (waiting) return true;
|
|
313
600
|
this.runsSkippedEmpty += 1;
|
|
314
|
-
log.info(`
|
|
601
|
+
log.info(`wake: the tray is empty \u2014 no run (nothing is owed; ${this.runsSkippedEmpty} skipped so far)`);
|
|
315
602
|
this.settleLadder();
|
|
316
603
|
this.onChange();
|
|
317
604
|
return false;
|
|
318
605
|
}
|
|
319
606
|
/**
|
|
320
|
-
* Nothing is owed any more — a
|
|
321
|
-
* found it empty: back to the first rung, drop a pending re-
|
|
607
|
+
* Nothing is owed any more — a wake run read the whole tray, or a count
|
|
608
|
+
* found it empty: back to the first rung, drop a pending re-wake (it would
|
|
322
609
|
* only ring for a backlog that is already handled), end the preflight watch.
|
|
323
610
|
*/
|
|
324
611
|
settleLadder() {
|
|
325
612
|
this.retryRung = 0;
|
|
326
|
-
if (this.coalescer.cancelDelayed()) log.info("pending re-
|
|
613
|
+
if (this.coalescer.cancelDelayed()) log.info("pending re-wake cancelled \u2014 nothing is owed any more");
|
|
327
614
|
this.clearPreflightWatch();
|
|
328
615
|
}
|
|
329
|
-
async run(kind,
|
|
616
|
+
async run(kind, invite) {
|
|
330
617
|
const runId = this.newRunId();
|
|
331
618
|
const startedAt = this.now();
|
|
332
619
|
this.runsStarted += 1;
|
|
@@ -334,9 +621,35 @@ var DoorbellInvoker = class {
|
|
|
334
621
|
log.info(`run start kind=${kind} id=${runId} budget=${Math.round(this.budgetMs / 1e3)}s`);
|
|
335
622
|
const controller = new AbortController();
|
|
336
623
|
this.inFlight = controller;
|
|
624
|
+
let offerRecorded = false;
|
|
625
|
+
const onStarted = () => {
|
|
626
|
+
if (invite === void 0 || offerRecorded) return;
|
|
627
|
+
offerRecorded = true;
|
|
628
|
+
this.offered.add(invite.inviteId);
|
|
629
|
+
};
|
|
337
630
|
let outcome;
|
|
338
631
|
try {
|
|
339
|
-
|
|
632
|
+
const prompts = await this.prompts();
|
|
633
|
+
if (prompts === null) {
|
|
634
|
+
outcome = {
|
|
635
|
+
state: "not_started",
|
|
636
|
+
exitCode: null,
|
|
637
|
+
durationMs: this.now() - startedAt,
|
|
638
|
+
detail: "no served texts in hand: GET /v1/texts has not answered yet and nothing is cached in the identity home"
|
|
639
|
+
};
|
|
640
|
+
} else if (this.stopped) {
|
|
641
|
+
outcome = { state: "not_started", exitCode: null, durationMs: this.now() - startedAt, detail: "interrupted before the runtime was started" };
|
|
642
|
+
} else {
|
|
643
|
+
const prompt = kind === "invite" ? prompts.invite : prompts.wake;
|
|
644
|
+
outcome = await this.runner.start({
|
|
645
|
+
kind,
|
|
646
|
+
prompt,
|
|
647
|
+
runId,
|
|
648
|
+
budgetMs: this.budgetMs,
|
|
649
|
+
signal: controller.signal,
|
|
650
|
+
...invite !== void 0 ? { onStarted } : {}
|
|
651
|
+
});
|
|
652
|
+
}
|
|
340
653
|
} catch (err) {
|
|
341
654
|
outcome = {
|
|
342
655
|
state: "not_started",
|
|
@@ -359,7 +672,7 @@ var DoorbellInvoker = class {
|
|
|
359
672
|
switch (outcome.state) {
|
|
360
673
|
case "ok":
|
|
361
674
|
this.runsOk += 1;
|
|
362
|
-
if (kind === "
|
|
675
|
+
if (kind === "wake") this.settleLadder();
|
|
363
676
|
log.info(`run ok kind=${kind} id=${runId} ${Math.round(outcome.durationMs / 1e3)}s (output discarded; any send was the agent's own hosted-tool call)`);
|
|
364
677
|
break;
|
|
365
678
|
case "failed":
|
|
@@ -372,33 +685,50 @@ var DoorbellInvoker = class {
|
|
|
372
685
|
this.runsNotStarted += 1;
|
|
373
686
|
break;
|
|
374
687
|
}
|
|
688
|
+
if (invite !== void 0) this.settleInvite(invite, outcome, offerRecorded);
|
|
375
689
|
if (outcome.state !== "ok") {
|
|
376
690
|
log.warn(`run ${outcome.state} kind=${kind} id=${runId}${outcome.detail ? `: ${outcome.detail}` : ""}`);
|
|
377
691
|
if (this.stopped) {
|
|
378
|
-
} else if (kind === "
|
|
692
|
+
} else if (kind === "wake") {
|
|
379
693
|
this.scheduleRetry(`run ${outcome.state}`);
|
|
380
|
-
} else {
|
|
381
|
-
log.info(`group-invite run ${outcome.state} \u2014 the invite stays pending server-side`);
|
|
382
694
|
}
|
|
383
695
|
}
|
|
384
696
|
this.onChange();
|
|
385
697
|
}
|
|
698
|
+
/**
|
|
699
|
+
* An invite counts as offered once its run has started (recorded at
|
|
700
|
+
* `onStarted`; a runner that never signals it is recorded here, when the
|
|
701
|
+
* run ends): the agent had its chance, and the invite stays reachable
|
|
702
|
+
* through the hosted invite tools in any later run — including a run this
|
|
703
|
+
* integration interrupted. A run that never got going is no offer: the
|
|
704
|
+
* record is taken back and the invite is held for the next wake.
|
|
705
|
+
*/
|
|
706
|
+
settleInvite(invite, outcome, offerRecorded) {
|
|
707
|
+
if (outcome.state === "not_started") {
|
|
708
|
+
if (offerRecorded) this.offered.remove(invite.inviteId);
|
|
709
|
+
if (!this.invitesAwaitingRetry.some((i) => i.inviteId === invite.inviteId)) this.invitesAwaitingRetry.push(invite);
|
|
710
|
+
log.info(`group-invite run did not start \u2014 invite ${invite.inviteId} is offered again on the next wake`);
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
if (!offerRecorded) this.offered.add(invite.inviteId);
|
|
714
|
+
if (outcome.state !== "ok") log.info(`group-invite run ${outcome.state} \u2014 the invite stays pending server-side, reachable through the hosted tools`);
|
|
715
|
+
}
|
|
386
716
|
scheduleRetry(reason) {
|
|
387
717
|
if (this.coalescer.delayedPending) {
|
|
388
|
-
log.info(`${reason} \u2014 a re-
|
|
718
|
+
log.info(`${reason} \u2014 a re-wake is already scheduled; not climbing the ladder`);
|
|
389
719
|
return;
|
|
390
720
|
}
|
|
391
721
|
if (this.retryRung >= this.ladder.length) {
|
|
392
722
|
log.warn(
|
|
393
|
-
`${reason} \u2014 retry ladder exhausted; the backlog stays
|
|
723
|
+
`${reason} \u2014 retry ladder exhausted; the backlog stays unread. Watching the runtime's preflight every ${Math.round(this.preflightWatchMs / 1e3)}s: if it comes back, one re-wake; any wake also re-runs`
|
|
394
724
|
);
|
|
395
725
|
this.armPreflightWatch();
|
|
396
726
|
return;
|
|
397
727
|
}
|
|
398
728
|
const delay = this.ladder[this.retryRung] ?? this.ladder[this.ladder.length - 1] ?? 6e4;
|
|
399
729
|
this.retryRung += 1;
|
|
400
|
-
log.warn(`${reason} \u2014 re-
|
|
401
|
-
this.coalescer.
|
|
730
|
+
log.warn(`${reason} \u2014 re-waking in ${Math.round(delay / 1e3)}s (rung ${this.retryRung}/${this.ladder.length})`);
|
|
731
|
+
this.coalescer.wakeAfter(delay);
|
|
402
732
|
}
|
|
403
733
|
// ─── preflight watch, after an exhausted ladder ─────────────────────────
|
|
404
734
|
armPreflightWatch() {
|
|
@@ -433,310 +763,64 @@ var DoorbellInvoker = class {
|
|
|
433
763
|
}
|
|
434
764
|
if (this.preflightWasDown) {
|
|
435
765
|
this.preflightWasDown = false;
|
|
436
|
-
log.info("runtime preflight is ok again after an exhausted ladder \u2014
|
|
437
|
-
this.
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
log.info("runtime preflight was already ok \u2014 the failures are not its readiness; the backlog waits for the next knock");
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
// src/doorbell/socket.ts
|
|
445
|
-
import { WebSocket } from "ws";
|
|
446
|
-
import { EventEmitter } from "events";
|
|
447
|
-
var BASE_BACKOFF_MS = 1e3;
|
|
448
|
-
var MAX_BACKOFF_MS = 6e4;
|
|
449
|
-
var LIVENESS_MS = 1e5;
|
|
450
|
-
var RECONNECT_HARD_CAP_ATTEMPTS = 500;
|
|
451
|
-
var HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
452
|
-
var AUTH_CLOSE_CODES = /* @__PURE__ */ new Set([1008, 4401, 4403]);
|
|
453
|
-
function stringField(record, keys) {
|
|
454
|
-
for (const key of keys) {
|
|
455
|
-
const value = record[key];
|
|
456
|
-
if (typeof value === "string" && value.length > 0) return value;
|
|
457
|
-
}
|
|
458
|
-
return null;
|
|
459
|
-
}
|
|
460
|
-
function asRecord(value) {
|
|
461
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
462
|
-
}
|
|
463
|
-
var DoorbellSocket = class extends EventEmitter {
|
|
464
|
-
constructor(options) {
|
|
465
|
-
super();
|
|
466
|
-
this.options = options;
|
|
467
|
-
this.selfHandle = options.selfHandle.trim().replace(/^@/, "").toLowerCase();
|
|
468
|
-
}
|
|
469
|
-
options;
|
|
470
|
-
ws = null;
|
|
471
|
-
_state = "idle";
|
|
472
|
-
attempt = 0;
|
|
473
|
-
reconnectTimer = null;
|
|
474
|
-
livenessTimer = null;
|
|
475
|
-
stopped = false;
|
|
476
|
-
_ackNegotiated = null;
|
|
477
|
-
/** `hello.ok` seen on the CURRENT connection. Reset on every open. */
|
|
478
|
-
helloOk = false;
|
|
479
|
-
selfHandle;
|
|
480
|
-
get state() {
|
|
481
|
-
return this._state;
|
|
482
|
-
}
|
|
483
|
-
/** True only while the socket is live — `hello.ok` received, not merely open. */
|
|
484
|
-
get connected() {
|
|
485
|
-
return this._state === "ready";
|
|
486
|
-
}
|
|
487
|
-
/** null until the server answered; then whether `ack` was echoed. */
|
|
488
|
-
get ackNegotiated() {
|
|
489
|
-
return this._ackNegotiated;
|
|
490
|
-
}
|
|
491
|
-
on(event, listener) {
|
|
492
|
-
return super.on(event, listener);
|
|
493
|
-
}
|
|
494
|
-
start() {
|
|
495
|
-
this.stopped = false;
|
|
496
|
-
this.open();
|
|
497
|
-
}
|
|
498
|
-
stop() {
|
|
499
|
-
this.stopped = true;
|
|
500
|
-
this.clearTimers();
|
|
501
|
-
if (this._state !== "terminal") this.setState("closed");
|
|
502
|
-
const ws = this.ws;
|
|
503
|
-
this.ws = null;
|
|
504
|
-
if (ws) {
|
|
505
|
-
try {
|
|
506
|
-
ws.close(1e3, "doorbell shutdown");
|
|
507
|
-
} catch {
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
setState(next) {
|
|
512
|
-
const prev = this._state;
|
|
513
|
-
if (prev === next) return;
|
|
514
|
-
this._state = next;
|
|
515
|
-
this.emit("state", next, prev);
|
|
516
|
-
}
|
|
517
|
-
open() {
|
|
518
|
-
if (this.stopped) return;
|
|
519
|
-
this.setState(this.attempt === 0 ? "connecting" : "reconnecting");
|
|
520
|
-
log.info(`doorbell socket ${this._state} (attempt ${this.attempt + 1})`);
|
|
521
|
-
const ws = new WebSocket(this.options.url, {
|
|
522
|
-
headers: {
|
|
523
|
-
...CODING_AGENTS_CLIENT_HEADERS,
|
|
524
|
-
authorization: `Bearer ${this.options.apiKey}`,
|
|
525
|
-
// Opt into the delivery-ack protocol: the server then leaves each
|
|
526
|
-
// envelope `stored` until an ack — which this socket never sends.
|
|
527
|
-
"x-agentchat-capabilities": "ack"
|
|
528
|
-
},
|
|
529
|
-
handshakeTimeout: HANDSHAKE_TIMEOUT_MS
|
|
530
|
-
});
|
|
531
|
-
this.ws = ws;
|
|
532
|
-
this.helloOk = false;
|
|
533
|
-
this._ackNegotiated = null;
|
|
534
|
-
const current = () => this.ws === ws;
|
|
535
|
-
ws.on("open", () => {
|
|
536
|
-
if (!current()) return;
|
|
537
|
-
this.armLiveness();
|
|
538
|
-
log.info("doorbell socket open \u2014 waiting for hello.ok");
|
|
539
|
-
});
|
|
540
|
-
ws.on("message", (data) => {
|
|
541
|
-
if (!current()) return;
|
|
542
|
-
this.armLiveness();
|
|
543
|
-
let frame;
|
|
544
|
-
try {
|
|
545
|
-
frame = JSON.parse(data.toString());
|
|
546
|
-
} catch {
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
const record = asRecord(frame);
|
|
550
|
-
if (record === null) return;
|
|
551
|
-
const type = record["type"];
|
|
552
|
-
if (type === "hello.ok") {
|
|
553
|
-
this.onHelloOk(record);
|
|
554
|
-
} else if (type === "message.new") {
|
|
555
|
-
this.onMessageFrame(asRecord(record["payload"]));
|
|
556
|
-
} else if (type === "group.invite.received") {
|
|
557
|
-
this.onInviteFrame(asRecord(record["payload"]));
|
|
558
|
-
} else {
|
|
559
|
-
log.debug(`doorbell socket frame ${String(type)} \u2014 not a knock`);
|
|
560
|
-
}
|
|
561
|
-
});
|
|
562
|
-
ws.on("ping", () => {
|
|
563
|
-
if (current()) this.armLiveness();
|
|
564
|
-
});
|
|
565
|
-
ws.on("unexpected-response", (req, res) => {
|
|
566
|
-
if (!current()) return;
|
|
567
|
-
const status = res.statusCode ?? 0;
|
|
568
|
-
try {
|
|
569
|
-
res.resume();
|
|
570
|
-
req.destroy();
|
|
571
|
-
} catch {
|
|
572
|
-
}
|
|
573
|
-
this.ws = null;
|
|
574
|
-
if (this.stopped) return;
|
|
575
|
-
if (status === 401 || status === 403) {
|
|
576
|
-
this.enterTerminal("auth", `auth rejected (HTTP ${status}) \u2014 check the agent's API key`);
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
log.warn(`doorbell socket unexpected response ${status} \u2014 scheduling reconnect`);
|
|
580
|
-
this.scheduleReconnect();
|
|
581
|
-
});
|
|
582
|
-
ws.on("error", (err) => {
|
|
583
|
-
if (!current()) return;
|
|
584
|
-
log.warn(`doorbell socket error: ${String(err)}`);
|
|
585
|
-
});
|
|
586
|
-
ws.on("close", (code, reasonBuffer) => {
|
|
587
|
-
if (!current()) return;
|
|
588
|
-
this.ws = null;
|
|
589
|
-
if (this._state === "terminal" || this.stopped) return;
|
|
590
|
-
const reason = reasonBuffer.toString();
|
|
591
|
-
if (!this.helloOk && AUTH_CLOSE_CODES.has(code)) {
|
|
592
|
-
this.enterTerminal("auth", `auth rejected (close ${code}${reason ? ` ${reason}` : ""}) \u2014 check the agent's API key`);
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
log.warn(`doorbell socket closed (${code}${reason ? ` ${reason}` : ""}) \u2014 scheduling reconnect`);
|
|
596
|
-
this.scheduleReconnect();
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
/** The server accepted the credential and echoed the capability set. */
|
|
600
|
-
onHelloOk(record) {
|
|
601
|
-
const caps = Array.isArray(record["capabilities"]) ? record["capabilities"] : [];
|
|
602
|
-
this._ackNegotiated = caps.includes("ack");
|
|
603
|
-
const first = !this.helloOk;
|
|
604
|
-
this.helloOk = true;
|
|
605
|
-
this.attempt = 0;
|
|
606
|
-
this.setState("ready");
|
|
607
|
-
log.info(`doorbell socket hello.ok \u2014 ack capability ${this._ackNegotiated ? "negotiated" : "NOT echoed (legacy server)"}`);
|
|
608
|
-
this.emit("ack", this._ackNegotiated);
|
|
609
|
-
if (first) this.emit("ready");
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* A `message.new` frame. Only the sender (echo filter) and the ids (log
|
|
613
|
-
* line) are read. `content`, `metadata`, previews — never touched.
|
|
614
|
-
*/
|
|
615
|
-
onMessageFrame(payload) {
|
|
616
|
-
if (payload === null) {
|
|
617
|
-
log.warn("doorbell socket: message.new without an object payload \u2014 dropped");
|
|
766
|
+
log.info("runtime preflight is ok again after an exhausted ladder \u2014 waking once");
|
|
767
|
+
this.wake("preflight");
|
|
618
768
|
return;
|
|
619
769
|
}
|
|
620
|
-
|
|
621
|
-
const sender = senderRaw === null ? null : senderRaw.replace(/^@/, "").toLowerCase();
|
|
622
|
-
const messageId = stringField(payload, ["id"]);
|
|
623
|
-
const conversationId = stringField(payload, ["conversation_id"]);
|
|
624
|
-
if (sender !== null && sender === this.selfHandle) {
|
|
625
|
-
log.debug(`doorbell socket: own echo in ${conversationId ?? "?"} \u2014 not a knock`);
|
|
626
|
-
this.emit("echo");
|
|
627
|
-
return;
|
|
628
|
-
}
|
|
629
|
-
log.info(`knock \u2014 message.new conv=${conversationId ?? "?"} msg=${messageId ?? "?"} from=@${sender ?? "?"}`);
|
|
630
|
-
this.emit("knock", { source: "message", sender, conversationId, messageId });
|
|
631
|
-
}
|
|
632
|
-
onInviteFrame(payload) {
|
|
633
|
-
if (payload === null) {
|
|
634
|
-
log.warn("doorbell socket: group.invite.received without an object payload \u2014 dropped");
|
|
635
|
-
return;
|
|
636
|
-
}
|
|
637
|
-
const inviteId = stringField(payload, ["invite_id", "id"]);
|
|
638
|
-
if (inviteId === null) {
|
|
639
|
-
log.warn("doorbell socket: group invite without an invite id \u2014 dropped");
|
|
640
|
-
return;
|
|
641
|
-
}
|
|
642
|
-
const invite = {
|
|
643
|
-
inviteId,
|
|
644
|
-
groupId: stringField(payload, ["group_id", "conversation_id"]),
|
|
645
|
-
groupName: stringField(payload, ["group_name", "name"]),
|
|
646
|
-
inviterHandle: stringField(payload, ["inviter_handle", "inviter", "from"])
|
|
647
|
-
};
|
|
648
|
-
log.info(`knock \u2014 group invite ${invite.inviteId} to ${invite.groupId ?? "?"} from=@${invite.inviterHandle ?? "?"}`);
|
|
649
|
-
this.emit("invite", invite);
|
|
650
|
-
}
|
|
651
|
-
/** Give up. The socket stays stopped; the holder decides what happens next. */
|
|
652
|
-
enterTerminal(reason, detail) {
|
|
653
|
-
this.clearTimers();
|
|
654
|
-
this.ws = null;
|
|
655
|
-
this.setState("terminal");
|
|
656
|
-
log.error(`doorbell socket terminal (${reason}): ${detail}`);
|
|
657
|
-
this.emit("terminal", reason, detail);
|
|
658
|
-
}
|
|
659
|
-
scheduleReconnect() {
|
|
660
|
-
if (this.stopped || this._state === "terminal") return;
|
|
661
|
-
if (this.reconnectTimer) return;
|
|
662
|
-
this.setState("reconnecting");
|
|
663
|
-
this.clearTimers();
|
|
664
|
-
const nextAttempt = this.attempt + 1;
|
|
665
|
-
if (nextAttempt > RECONNECT_HARD_CAP_ATTEMPTS) {
|
|
666
|
-
this.enterTerminal("cap", `reconnect hard cap (${RECONNECT_HARD_CAP_ATTEMPTS} attempts) reached \u2014 the control plane is unreachable from here`);
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
const backoff = Math.min(BASE_BACKOFF_MS * 2 ** Math.min(this.attempt, 20), MAX_BACKOFF_MS);
|
|
670
|
-
const jitter = backoff * (0.5 + Math.random() * 0.5);
|
|
671
|
-
this.attempt = nextAttempt;
|
|
672
|
-
this.reconnectTimer = setTimeout(() => {
|
|
673
|
-
this.reconnectTimer = null;
|
|
674
|
-
this.open();
|
|
675
|
-
}, jitter);
|
|
676
|
-
}
|
|
677
|
-
armLiveness() {
|
|
678
|
-
if (this.livenessTimer) clearTimeout(this.livenessTimer);
|
|
679
|
-
this.livenessTimer = setTimeout(() => {
|
|
680
|
-
log.warn("doorbell socket liveness timeout \u2014 forcing reconnect");
|
|
681
|
-
const ws = this.ws;
|
|
682
|
-
this.ws = null;
|
|
683
|
-
try {
|
|
684
|
-
ws?.terminate();
|
|
685
|
-
} catch {
|
|
686
|
-
}
|
|
687
|
-
this.scheduleReconnect();
|
|
688
|
-
}, LIVENESS_MS);
|
|
689
|
-
}
|
|
690
|
-
clearTimers() {
|
|
691
|
-
if (this.reconnectTimer) {
|
|
692
|
-
clearTimeout(this.reconnectTimer);
|
|
693
|
-
this.reconnectTimer = null;
|
|
694
|
-
}
|
|
695
|
-
if (this.livenessTimer) {
|
|
696
|
-
clearTimeout(this.livenessTimer);
|
|
697
|
-
this.livenessTimer = null;
|
|
698
|
-
}
|
|
770
|
+
log.info("runtime preflight was already ok \u2014 the failures are not its readiness; the backlog waits for the next wake");
|
|
699
771
|
}
|
|
700
772
|
};
|
|
701
773
|
|
|
702
|
-
// src/
|
|
774
|
+
// src/integration/integration.ts
|
|
703
775
|
var HEARTBEAT_MS = 3e4;
|
|
704
776
|
var STATUS_MS = 3e4;
|
|
705
|
-
var
|
|
777
|
+
var HTTP_TIMEOUT_MS = 1e4;
|
|
778
|
+
var Integration = class {
|
|
706
779
|
constructor(options) {
|
|
707
780
|
this.options = options;
|
|
708
|
-
const { cfg } = options;
|
|
709
|
-
|
|
710
|
-
|
|
781
|
+
const { cfg, client } = options;
|
|
782
|
+
this.api = options.api ?? new AgentChatClient2({
|
|
783
|
+
apiKey: cfg.apiKey,
|
|
784
|
+
baseUrl: cfg.apiBase,
|
|
785
|
+
clientIdentity: clientIdentityOf(client),
|
|
786
|
+
timeoutMs: HTTP_TIMEOUT_MS,
|
|
787
|
+
retry: { maxRetries: 0, baseDelayMs: 0, maxDelayMs: 0 }
|
|
788
|
+
});
|
|
789
|
+
this.texts = options.texts ?? new TextsStore({ home: cfg.home, runtime: client.runtime, client: this.api, onChange: () => this.writeStatus() });
|
|
790
|
+
this.invoker = new IntegrationInvoker({
|
|
711
791
|
runner: options.runner,
|
|
712
|
-
prompts:
|
|
792
|
+
prompts: () => this.runPrompts(),
|
|
713
793
|
budgetMs: options.budgetMs,
|
|
714
794
|
onChange: () => this.writeStatus(),
|
|
715
|
-
trayHasWaiting: () => trayHasWaiting(
|
|
795
|
+
trayHasWaiting: () => trayHasWaiting(this.api),
|
|
796
|
+
offeredInvites: OfferedInvites.load(cfg.home)
|
|
716
797
|
});
|
|
717
|
-
this.
|
|
718
|
-
this.
|
|
719
|
-
this.
|
|
798
|
+
this.connection = options.connection ?? new IntegrationConnection({ apiKey: cfg.apiKey, wsBase: cfg.wsBase, selfHandle: cfg.handle, client });
|
|
799
|
+
this.connection.on("wake", (wake) => this.invoker.wake(wake.source));
|
|
800
|
+
this.connection.on("invite", (invite) => {
|
|
720
801
|
this.invoker.invite(invite);
|
|
721
802
|
});
|
|
722
|
-
this.
|
|
803
|
+
this.connection.on("ready", () => {
|
|
723
804
|
void this.catchUp();
|
|
724
805
|
});
|
|
725
|
-
this.
|
|
726
|
-
if (state === "ready")
|
|
727
|
-
|
|
806
|
+
this.connection.on("state", (state) => {
|
|
807
|
+
if (state === "ready") {
|
|
808
|
+
beat(cfg.home);
|
|
809
|
+
this.lastError = null;
|
|
810
|
+
} else {
|
|
811
|
+
idle(cfg.home);
|
|
812
|
+
}
|
|
728
813
|
this.writeStatus();
|
|
729
814
|
});
|
|
730
|
-
this.
|
|
731
|
-
this.socket.on("terminal", (reason, detail) => {
|
|
815
|
+
this.connection.on("blocked", (detail) => {
|
|
732
816
|
this.lastError = detail;
|
|
733
|
-
|
|
734
|
-
this.stop();
|
|
735
|
-
options.onTerminal?.(reason, detail);
|
|
817
|
+
this.writeStatus();
|
|
736
818
|
});
|
|
737
819
|
}
|
|
738
820
|
options;
|
|
739
|
-
|
|
821
|
+
api;
|
|
822
|
+
connection;
|
|
823
|
+
texts;
|
|
740
824
|
invoker;
|
|
741
825
|
heartbeatTimer = null;
|
|
742
826
|
statusTimer = null;
|
|
@@ -748,11 +832,13 @@ var Doorbell = class {
|
|
|
748
832
|
throw new Error(`runtime (${this.options.runner.name}) not ready: ${pre.detail ?? "unknown"}`);
|
|
749
833
|
}
|
|
750
834
|
log.info(
|
|
751
|
-
`
|
|
835
|
+
`integration up as @${this.options.cfg.handle} via ${this.options.runner.name} (${this.options.client.runtime} ${this.options.client.version}), profile ${this.options.profile}; fetching the served texts, then holding the socket`
|
|
752
836
|
);
|
|
753
|
-
this.
|
|
837
|
+
await this.texts.start();
|
|
838
|
+
if (this.stopping) return;
|
|
839
|
+
this.connection.start();
|
|
754
840
|
this.heartbeatTimer = setInterval(() => {
|
|
755
|
-
if (this.
|
|
841
|
+
if (this.connection.connected) beat(this.options.cfg.home);
|
|
756
842
|
}, HEARTBEAT_MS);
|
|
757
843
|
this.heartbeatTimer.unref();
|
|
758
844
|
this.statusTimer = setInterval(() => this.writeStatus(), STATUS_MS);
|
|
@@ -770,7 +856,8 @@ var Doorbell = class {
|
|
|
770
856
|
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
|
|
771
857
|
if (this.statusTimer) clearInterval(this.statusTimer);
|
|
772
858
|
this.invoker.stop();
|
|
773
|
-
this.
|
|
859
|
+
this.texts.stop();
|
|
860
|
+
this.connection.stop();
|
|
774
861
|
idle(this.options.cfg.home);
|
|
775
862
|
this.writeStatus();
|
|
776
863
|
}
|
|
@@ -800,18 +887,18 @@ var Doorbell = class {
|
|
|
800
887
|
}
|
|
801
888
|
snapshot() {
|
|
802
889
|
const c = this.invoker.counters();
|
|
803
|
-
const socket = this.
|
|
890
|
+
const socket = this.stopping ? "closed" : this.connection.state;
|
|
891
|
+
const texts = this.texts.current();
|
|
804
892
|
return {
|
|
805
|
-
version:
|
|
893
|
+
version: 2,
|
|
806
894
|
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
807
895
|
pid: process.pid,
|
|
808
896
|
runtime: this.options.runner.name,
|
|
809
897
|
handle: this.options.cfg.handle,
|
|
810
898
|
profile: this.options.profile,
|
|
811
899
|
socket,
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
knocks_absorbed: c.absorbed,
|
|
900
|
+
wakes: c.wakes,
|
|
901
|
+
wakes_absorbed: c.absorbed,
|
|
815
902
|
invites: c.invites,
|
|
816
903
|
runs_started: c.runsStarted,
|
|
817
904
|
runs_ok: c.runsOk,
|
|
@@ -822,41 +909,56 @@ var Doorbell = class {
|
|
|
822
909
|
last_run: c.lastRun,
|
|
823
910
|
retry_rung: c.retryRung,
|
|
824
911
|
retry_due_at: c.retryDueAt === null ? null : new Date(c.retryDueAt).toISOString(),
|
|
825
|
-
last_error: this.lastError
|
|
912
|
+
last_error: this.lastError,
|
|
913
|
+
texts: {
|
|
914
|
+
source: this.texts.source,
|
|
915
|
+
fetched_at: this.texts.fetchedAt,
|
|
916
|
+
version: texts?.version ?? null,
|
|
917
|
+
manual_version: texts?.manual.version ?? null
|
|
918
|
+
}
|
|
826
919
|
};
|
|
827
920
|
}
|
|
921
|
+
/** The served texts plus the pointer line, or null while no text is in hand. */
|
|
922
|
+
async runPrompts() {
|
|
923
|
+
const texts = await this.texts.ensure();
|
|
924
|
+
return texts === null ? null : buildRunPrompts(texts, this.options.manualPointer);
|
|
925
|
+
}
|
|
828
926
|
/**
|
|
829
|
-
* The connect-time catch-up: one
|
|
927
|
+
* The connect-time catch-up: one wake (the invoker counts the tray before
|
|
830
928
|
* any run, so an empty tray costs a count, not a run) and the pending invites.
|
|
831
929
|
*/
|
|
832
930
|
async catchUp() {
|
|
833
|
-
const { cfg } = this.options;
|
|
834
|
-
const wire = { apiKey: cfg.apiKey, apiBase: cfg.apiBase };
|
|
835
931
|
if (this.stopping) return;
|
|
836
|
-
log.info("
|
|
837
|
-
this.invoker.
|
|
932
|
+
log.info("wake \u2014 connect (a run starts only if the tray has something unread)");
|
|
933
|
+
this.invoker.wake("connect");
|
|
838
934
|
try {
|
|
839
|
-
const invites = await listGroupInvites(
|
|
935
|
+
const invites = await this.api.listGroupInvites();
|
|
840
936
|
if (this.stopping) return;
|
|
841
937
|
let surfaced = 0;
|
|
842
|
-
for (const
|
|
843
|
-
|
|
938
|
+
for (const row of invites) {
|
|
939
|
+
const invite = {
|
|
940
|
+
inviteId: row.id,
|
|
941
|
+
groupId: row.group_id ?? null,
|
|
942
|
+
groupName: row.group_name ?? null,
|
|
943
|
+
inviterHandle: row.inviter_handle ?? null
|
|
944
|
+
};
|
|
945
|
+
if (this.invoker.invite(invite)) surfaced += 1;
|
|
946
|
+
}
|
|
947
|
+
if (surfaced > 0) log.info(`wake \u2014 ${surfaced} pending group invite(s) surfaced on connect`);
|
|
844
948
|
} catch (err) {
|
|
845
949
|
log.warn(`connect-time invite list failed (${err instanceof Error ? err.message : String(err)}) \u2014 live invite frames are unaffected`);
|
|
846
950
|
}
|
|
847
951
|
this.writeStatus();
|
|
848
952
|
}
|
|
849
953
|
writeStatus() {
|
|
850
|
-
|
|
954
|
+
writeIntegrationStatus(this.options.cfg.home, this.snapshot());
|
|
851
955
|
}
|
|
852
956
|
};
|
|
853
957
|
|
|
854
|
-
// src/
|
|
958
|
+
// src/integration/run.ts
|
|
855
959
|
var POLL_MS = 5e3;
|
|
856
960
|
var TICK_MS = 250;
|
|
857
|
-
var
|
|
858
|
-
var SOCKET_GIVEUP_HOLD_BASE_MS = 6e4;
|
|
859
|
-
var SOCKET_GIVEUP_HOLD_MAX_MS = 10 * 6e4;
|
|
961
|
+
var MAX_BACKOFF_MS = 5 * 6e4;
|
|
860
962
|
var SHUTDOWN_GRACE_MS = 1e4;
|
|
861
963
|
var MAX_LOG_BYTES = 5 * 1024 * 1024;
|
|
862
964
|
var KEEP_LOG_BYTES = 1024 * 1024;
|
|
@@ -866,7 +968,7 @@ function fingerprint(home) {
|
|
|
866
968
|
return id === null ? null : `${id.apiKey}:${id.handle ?? ""}`;
|
|
867
969
|
}
|
|
868
970
|
function boundDaemonLog(home) {
|
|
869
|
-
const file =
|
|
971
|
+
const file = path3.join(home, "daemon.log");
|
|
870
972
|
try {
|
|
871
973
|
const size = fs.statSync(file).size;
|
|
872
974
|
if (size <= MAX_LOG_BYTES) return;
|
|
@@ -885,9 +987,9 @@ ${keep.toString("utf-8")}`
|
|
|
885
987
|
} catch {
|
|
886
988
|
}
|
|
887
989
|
}
|
|
888
|
-
async function
|
|
889
|
-
const home =
|
|
890
|
-
const workdir = opts.workdir
|
|
990
|
+
async function runIntegration(opts) {
|
|
991
|
+
const home = path3.resolve(opts.home);
|
|
992
|
+
const workdir = path3.resolve(opts.workdir);
|
|
891
993
|
const env = opts.env ?? process.env;
|
|
892
994
|
const profile = opts.profile ?? resolveRunProfile(env);
|
|
893
995
|
const budgetMs = opts.budgetMs ?? resolveRunBudgetMs(env);
|
|
@@ -895,27 +997,24 @@ async function runDoorbell(opts) {
|
|
|
895
997
|
if (process.env["AGENTCHAT_LOG_LEVEL"] === void 0) process.env["AGENTCHAT_LOG_LEVEL"] = "info";
|
|
896
998
|
const lock = acquireLeaderLock(home);
|
|
897
999
|
if (lock === null) return 1;
|
|
898
|
-
const
|
|
1000
|
+
const disabledBy = integrationDisabledBy(env);
|
|
1001
|
+
const disabled = disabledBy !== null;
|
|
899
1002
|
let live = null;
|
|
900
1003
|
let liveFingerprint = null;
|
|
901
1004
|
let observedFingerprint = null;
|
|
902
|
-
let refused = null;
|
|
903
1005
|
let failures = 0;
|
|
904
|
-
let socketGiveUps = 0;
|
|
905
|
-
let holdUntil = 0;
|
|
906
1006
|
let lastFailure = null;
|
|
907
1007
|
let shuttingDown = false;
|
|
908
|
-
const offStatus = (handle) =>
|
|
909
|
-
version:
|
|
1008
|
+
const offStatus = (handle) => writeIntegrationStatus(home, {
|
|
1009
|
+
version: 2,
|
|
910
1010
|
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
911
1011
|
pid: process.pid,
|
|
912
1012
|
runtime: opts.runner.name,
|
|
913
1013
|
handle,
|
|
914
1014
|
profile,
|
|
915
1015
|
socket: disabled ? "off" : "idle",
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
knocks_absorbed: 0,
|
|
1016
|
+
wakes: 0,
|
|
1017
|
+
wakes_absorbed: 0,
|
|
919
1018
|
invites: 0,
|
|
920
1019
|
runs_started: 0,
|
|
921
1020
|
runs_ok: 0,
|
|
@@ -926,7 +1025,8 @@ async function runDoorbell(opts) {
|
|
|
926
1025
|
last_run: null,
|
|
927
1026
|
retry_rung: 0,
|
|
928
1027
|
retry_due_at: null,
|
|
929
|
-
last_error: lastFailure
|
|
1028
|
+
last_error: lastFailure,
|
|
1029
|
+
texts: null
|
|
930
1030
|
});
|
|
931
1031
|
const disconnect = (why) => {
|
|
932
1032
|
if (live === null) return;
|
|
@@ -961,9 +1061,9 @@ async function runDoorbell(opts) {
|
|
|
961
1061
|
watcher.unref();
|
|
962
1062
|
} catch {
|
|
963
1063
|
}
|
|
964
|
-
log.info(`
|
|
1064
|
+
log.info(`integration resident for ${home} (${credentialsPath(home)}) \u2014 ${opts.client.runtime} ${opts.client.version}`);
|
|
965
1065
|
if (disabled) {
|
|
966
|
-
log.info(
|
|
1066
|
+
log.info(`${disabledBy}=off \u2014 holding no socket and starting no run; the hosted tools and the manual still work in every session`);
|
|
967
1067
|
}
|
|
968
1068
|
idle(home);
|
|
969
1069
|
offStatus(null);
|
|
@@ -975,63 +1075,41 @@ async function runDoorbell(opts) {
|
|
|
975
1075
|
disconnect(fp === null ? "signed out" : "identity changed");
|
|
976
1076
|
observedFingerprint = fp;
|
|
977
1077
|
failures = 0;
|
|
978
|
-
socketGiveUps = 0;
|
|
979
|
-
holdUntil = 0;
|
|
980
1078
|
lastFailure = null;
|
|
981
|
-
if (fp !== refused) refused = null;
|
|
982
1079
|
if (fp === null || disabled) offStatus(fp === null ? null : resolveIdentity(home)?.handle ?? null);
|
|
983
1080
|
}
|
|
984
1081
|
if (fp === null) {
|
|
985
|
-
if (refused !== null) refused = null;
|
|
986
1082
|
} else if (disabled) {
|
|
987
1083
|
} else if (fp !== liveFingerprint) {
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
socketGiveUps += 1;
|
|
1010
|
-
const hold = Math.min(SOCKET_GIVEUP_HOLD_BASE_MS * 2 ** (socketGiveUps - 1), SOCKET_GIVEUP_HOLD_MAX_MS);
|
|
1011
|
-
holdUntil = Date.now() + hold;
|
|
1012
|
-
log.warn(`socket gave up (${detail}) \u2014 opening a fresh socket in ${Math.round(hold / 1e3)}s`);
|
|
1013
|
-
}
|
|
1014
|
-
});
|
|
1015
|
-
await candidate.start();
|
|
1016
|
-
live = candidate;
|
|
1017
|
-
liveFingerprint = fp;
|
|
1018
|
-
failures = 0;
|
|
1019
|
-
lastFailure = null;
|
|
1020
|
-
} catch (err) {
|
|
1021
|
-
const msg = String(err instanceof Error ? err.message : err);
|
|
1022
|
-
if (msg !== lastFailure) {
|
|
1023
|
-
log.warn(`not connecting yet: ${msg}`);
|
|
1024
|
-
lastFailure = msg;
|
|
1025
|
-
offStatus(resolveIdentity(home)?.handle ?? null);
|
|
1026
|
-
}
|
|
1027
|
-
failures += 1;
|
|
1028
|
-
live = null;
|
|
1029
|
-
liveFingerprint = null;
|
|
1030
|
-
idle(home);
|
|
1084
|
+
try {
|
|
1085
|
+
const cfg = await resolveDaemonConfig({ home, workdir, client: opts.client });
|
|
1086
|
+
const candidate = new Integration({
|
|
1087
|
+
cfg,
|
|
1088
|
+
runner: opts.runner,
|
|
1089
|
+
client: opts.client,
|
|
1090
|
+
manualPointer: opts.manualPointer,
|
|
1091
|
+
profile,
|
|
1092
|
+
budgetMs
|
|
1093
|
+
});
|
|
1094
|
+
await candidate.start();
|
|
1095
|
+
live = candidate;
|
|
1096
|
+
liveFingerprint = fp;
|
|
1097
|
+
failures = 0;
|
|
1098
|
+
lastFailure = null;
|
|
1099
|
+
} catch (err) {
|
|
1100
|
+
const msg = String(err instanceof Error ? err.message : err);
|
|
1101
|
+
if (msg !== lastFailure) {
|
|
1102
|
+
log.warn(`not connecting yet: ${msg}`);
|
|
1103
|
+
lastFailure = msg;
|
|
1104
|
+
offStatus(resolveIdentity(home)?.handle ?? null);
|
|
1031
1105
|
}
|
|
1106
|
+
failures += 1;
|
|
1107
|
+
live = null;
|
|
1108
|
+
liveFingerprint = null;
|
|
1109
|
+
idle(home);
|
|
1032
1110
|
}
|
|
1033
1111
|
}
|
|
1034
|
-
const waitMs = failures === 0 ? POLL_MS : Math.min(POLL_MS * 2 ** Math.min(failures, 6),
|
|
1112
|
+
const waitMs = failures === 0 ? POLL_MS : Math.min(POLL_MS * 2 ** Math.min(failures, 6), MAX_BACKOFF_MS);
|
|
1035
1113
|
nudged = false;
|
|
1036
1114
|
const deadline = Date.now() + waitMs;
|
|
1037
1115
|
while (!nudged && !shuttingDown && !opts.signal?.aborted && Date.now() < deadline) {
|
|
@@ -1054,7 +1132,7 @@ async function runDoorbell(opts) {
|
|
|
1054
1132
|
return 0;
|
|
1055
1133
|
}
|
|
1056
1134
|
|
|
1057
|
-
// src/
|
|
1135
|
+
// src/integration/spawn-run.ts
|
|
1058
1136
|
var DEFAULT_GRACE_MS = 5e3;
|
|
1059
1137
|
var STDERR_TAIL_CHARS = 400;
|
|
1060
1138
|
function redactDiagnostic(value, max = STDERR_TAIL_CHARS) {
|
|
@@ -1114,6 +1192,15 @@ function spawnRun(spec) {
|
|
|
1114
1192
|
finish({ state: "not_started", exitCode: null, detail: redactDiagnostic(String(err)) });
|
|
1115
1193
|
return;
|
|
1116
1194
|
}
|
|
1195
|
+
if (spec.onStarted) {
|
|
1196
|
+
const onStarted = spec.onStarted;
|
|
1197
|
+
child.once("spawn", () => {
|
|
1198
|
+
try {
|
|
1199
|
+
onStarted();
|
|
1200
|
+
} catch {
|
|
1201
|
+
}
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1117
1204
|
child.stdout?.on("data", (chunk) => {
|
|
1118
1205
|
stdoutBytes += chunk.length;
|
|
1119
1206
|
});
|
|
@@ -1163,7 +1250,7 @@ function spawnRun(spec) {
|
|
|
1163
1250
|
finish({
|
|
1164
1251
|
state: "failed",
|
|
1165
1252
|
exitCode: code,
|
|
1166
|
-
detail: `interrupted \u2014 the
|
|
1253
|
+
detail: `interrupted \u2014 the integration was stopping; the tray stays unread for the next run${tail ? `: ${tail}` : ""}`
|
|
1167
1254
|
});
|
|
1168
1255
|
return;
|
|
1169
1256
|
}
|
|
@@ -1181,17 +1268,22 @@ function spawnRun(spec) {
|
|
|
1181
1268
|
});
|
|
1182
1269
|
}
|
|
1183
1270
|
export {
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1271
|
+
DEFAULT_WAKE_DEBOUNCE_MS,
|
|
1272
|
+
Integration,
|
|
1273
|
+
IntegrationConnection,
|
|
1274
|
+
IntegrationInvoker,
|
|
1275
|
+
OFFERED_INVITES_FILE,
|
|
1276
|
+
OfferedInvites,
|
|
1189
1277
|
PREFLIGHT_WATCH_MS,
|
|
1190
1278
|
RETRY_LADDER_MS,
|
|
1279
|
+
WakeCoalescer,
|
|
1280
|
+
offeredInvitesPath,
|
|
1191
1281
|
redactDiagnostic,
|
|
1192
1282
|
resolveDaemonConfig,
|
|
1193
|
-
|
|
1283
|
+
runIntegration,
|
|
1284
|
+
socketStateOf,
|
|
1194
1285
|
spawnRun,
|
|
1286
|
+
wsBaseFor,
|
|
1195
1287
|
wsUrlFor
|
|
1196
1288
|
};
|
|
1197
1289
|
//# sourceMappingURL=daemon-entry.js.map
|