agent-coord-mcp 0.14.0 → 0.16.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 +49 -5
- package/dist/server.js +45 -5
- package/dist/server.js.map +1 -1
- package/dist/store.js +58 -0
- package/dist/store.js.map +1 -1
- package/dist/tools/admin.js +296 -0
- package/dist/tools/admin.js.map +1 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/messaging.js +317 -0
- package/dist/tools/messaging.js.map +1 -0
- package/dist/tools/registry.js +237 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/rooms.js +136 -0
- package/dist/tools/rooms.js.map +1 -0
- package/dist/tools/shared.js +61 -0
- package/dist/tools/shared.js.map +1 -0
- package/dist/tools/transport.js +1003 -0
- package/dist/tools/transport.js.map +1 -0
- package/hooks/tier.mjs +23 -3
- package/hooks/tmux-pusher.mjs +50 -6
- package/package.json +1 -1
- package/scripts/coord-chat.mjs +1 -1
- package/scripts/coord-node.sh +98 -0
- package/scripts/coord-pusher.mjs +37 -4
- package/scripts/coord-token.mjs +126 -0
- package/src/server.ts +54 -5
- package/src/store.ts +62 -0
- package/src/tools/admin.ts +347 -0
- package/src/tools/index.ts +6 -0
- package/src/tools/messaging.ts +425 -0
- package/src/tools/registry.ts +329 -0
- package/src/tools/rooms.ts +215 -0
- package/src/tools/shared.ts +167 -0
- package/src/{tools.ts → tools/transport.ts} +23 -1008
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { adjustCursors } from "./admin.js";
|
|
2
|
+
import { ARCHIVE_STATUS_FILE, archiveJsonl, archiveRoomFile } from "../store.js";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { existsSync, openSync, watch } from "node:fs";
|
|
5
|
+
import { promises as fsp } from "node:fs";
|
|
6
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
AGENTS_FILE,
|
|
12
|
+
CURSOR_DIR,
|
|
13
|
+
DEFAULT_ROOM,
|
|
14
|
+
INBOX_DIR,
|
|
15
|
+
ROOT,
|
|
16
|
+
ROOM_FILE,
|
|
17
|
+
ROOMS_DIR,
|
|
18
|
+
ROOMS_FILE,
|
|
19
|
+
STATUS_FILE,
|
|
20
|
+
addMember,
|
|
21
|
+
appendJsonl,
|
|
22
|
+
cursorFile,
|
|
23
|
+
deleteFile,
|
|
24
|
+
ensureRoom,
|
|
25
|
+
fileSize,
|
|
26
|
+
getRooms,
|
|
27
|
+
inboxFile,
|
|
28
|
+
listCursorFiles,
|
|
29
|
+
listInboxFiles,
|
|
30
|
+
listTransportFiles,
|
|
31
|
+
logFile,
|
|
32
|
+
memberRooms,
|
|
33
|
+
normalizeRoom,
|
|
34
|
+
pidFile,
|
|
35
|
+
readJson,
|
|
36
|
+
readJsonl,
|
|
37
|
+
receiptFile,
|
|
38
|
+
listReceiptFiles,
|
|
39
|
+
removeMember,
|
|
40
|
+
rewriteJsonl,
|
|
41
|
+
roomFile,
|
|
42
|
+
rotateAgentToken,
|
|
43
|
+
setRoomMeta,
|
|
44
|
+
stashHistory,
|
|
45
|
+
retrieveHistory,
|
|
46
|
+
pruneHistory,
|
|
47
|
+
transportFile,
|
|
48
|
+
TRANSPORT_DIR,
|
|
49
|
+
updateJson,
|
|
50
|
+
type RoomRegistry,
|
|
51
|
+
} from "../store.js";
|
|
52
|
+
import {
|
|
53
|
+
type AgentEntry,
|
|
54
|
+
type AgentRegistry,
|
|
55
|
+
type Message,
|
|
56
|
+
type StatusEntry,
|
|
57
|
+
type Cursor,
|
|
58
|
+
type Source,
|
|
59
|
+
type TransportMarker,
|
|
60
|
+
sourceFile,
|
|
61
|
+
getOffset,
|
|
62
|
+
setOffset,
|
|
63
|
+
sysMsg,
|
|
64
|
+
moveFile,
|
|
65
|
+
STALE_MS,
|
|
66
|
+
EVICT_MS,
|
|
67
|
+
MAX_WAIT_MS,
|
|
68
|
+
} from "./shared.js";
|
|
69
|
+
|
|
70
|
+
// ---------- send_message ----------
|
|
71
|
+
|
|
72
|
+
export const sendMessageSchema = {
|
|
73
|
+
from: z.string().min(1),
|
|
74
|
+
to: z.string().optional(),
|
|
75
|
+
room: z.string().optional(),
|
|
76
|
+
text: z.string().min(1),
|
|
77
|
+
kind: z.enum(["decision", "status", "chatter"]).optional(),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export async function sendMessageTool(args: {
|
|
81
|
+
from: string;
|
|
82
|
+
to?: string;
|
|
83
|
+
room?: string;
|
|
84
|
+
text: string;
|
|
85
|
+
kind?: "decision" | "status" | "chatter";
|
|
86
|
+
}) {
|
|
87
|
+
// DM → inbox. Otherwise resolve the channel (default `general`), make sure it
|
|
88
|
+
// exists in the registry, and tag the message with its channel.
|
|
89
|
+
if (args.to) {
|
|
90
|
+
const msg: Message = {
|
|
91
|
+
id: randomUUID(),
|
|
92
|
+
ts: Date.now(),
|
|
93
|
+
from: args.from,
|
|
94
|
+
to: args.to,
|
|
95
|
+
text: args.text,
|
|
96
|
+
};
|
|
97
|
+
const target = inboxFile(args.to);
|
|
98
|
+
await appendJsonl(target, msg);
|
|
99
|
+
// Offline delivery is intentional (the inbox is created on demand), but a
|
|
100
|
+
// typo'd recipient shouldn't vanish silently — surface a warning when the
|
|
101
|
+
// target isn't a known agent so the caller can catch the mistake.
|
|
102
|
+
const reg = await readJson<AgentRegistry>(AGENTS_FILE, {});
|
|
103
|
+
const warning = reg[args.to]
|
|
104
|
+
? undefined
|
|
105
|
+
: `recipient '${args.to}' is not a registered agent — message stored in their inbox but no one may be listening`;
|
|
106
|
+
return { ok: true, id: msg.id, target, room: undefined, warning };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const chan = normalizeRoom(args.room);
|
|
110
|
+
if (chan !== DEFAULT_ROOM) await ensureRoom(chan, args.from);
|
|
111
|
+
const msg: Message = {
|
|
112
|
+
id: randomUUID(),
|
|
113
|
+
ts: Date.now(),
|
|
114
|
+
from: args.from,
|
|
115
|
+
room: chan,
|
|
116
|
+
text: args.text,
|
|
117
|
+
...(args.kind ? { kind: args.kind } : {}),
|
|
118
|
+
};
|
|
119
|
+
const target = roomFile(chan);
|
|
120
|
+
await appendJsonl(target, msg);
|
|
121
|
+
await maybeCompactRoom(chan);
|
|
122
|
+
return { ok: true, id: msg.id, target, room: chan };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ---------- live compaction (self-limiting streams) ----------
|
|
126
|
+
|
|
127
|
+
// Rooms and the status stream compact themselves on write: once a live file
|
|
128
|
+
// grows past its threshold, the oldest entries move to the archive (never
|
|
129
|
+
// deleted) and every cursor shifts down. Fresh decisions (< decisionDays old)
|
|
130
|
+
// are exempt — they stay in the live file. Cursor adjustment subtracts the
|
|
131
|
+
// full removed count, so an agent parked behind a kept decision may re-read
|
|
132
|
+
// it once; at-least-once beats silently skipping.
|
|
133
|
+
function envInt(name: string, fallback: number): number {
|
|
134
|
+
const n = Number(process.env[name]);
|
|
135
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const ROOM_COMPACT_THRESHOLD = envInt("AGENT_COORD_ROOM_MAX", 1000);
|
|
139
|
+
const ROOM_COMPACT_KEEP = envInt("AGENT_COORD_ROOM_KEEP", 500);
|
|
140
|
+
const STATUS_COMPACT_THRESHOLD = envInt("AGENT_COORD_STATUS_MAX", 2000);
|
|
141
|
+
const STATUS_COMPACT_KEEP = envInt("AGENT_COORD_STATUS_KEEP", 1000);
|
|
142
|
+
// Skip the entry count entirely while the file is small — the common case on
|
|
143
|
+
// every send. ~150 bytes/entry means the threshold can't be hit below this.
|
|
144
|
+
const COMPACT_SIZE_GATE = 100 * 1024;
|
|
145
|
+
const DECISION_FRESH_MS = 30 * 24 * 60 * 60 * 1000;
|
|
146
|
+
|
|
147
|
+
async function maybeCompactRoom(chan: string): Promise<void> {
|
|
148
|
+
const file = roomFile(chan);
|
|
149
|
+
if ((await fileSize(file)) < COMPACT_SIZE_GATE) return;
|
|
150
|
+
const entries = await readJsonl<Message>(file);
|
|
151
|
+
if (entries.length <= ROOM_COMPACT_THRESHOLD) return;
|
|
152
|
+
const boundaryTs = entries[entries.length - ROOM_COMPACT_KEEP]!.ts;
|
|
153
|
+
const decisionCutoff = Date.now() - DECISION_FRESH_MS;
|
|
154
|
+
const r = await archiveJsonl<Message>(
|
|
155
|
+
file,
|
|
156
|
+
archiveRoomFile(chan),
|
|
157
|
+
(e) => e.ts >= boundaryTs || (e.kind === "decision" && e.ts > decisionCutoff)
|
|
158
|
+
);
|
|
159
|
+
if (r.removed > 0) await adjustCursors({ roomRemovedByChan: { [chan]: r.removed } });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function maybeCompactStatus(): Promise<void> {
|
|
163
|
+
if ((await fileSize(STATUS_FILE)) < COMPACT_SIZE_GATE) return;
|
|
164
|
+
const entries = await readJsonl<StatusEntry>(STATUS_FILE);
|
|
165
|
+
if (entries.length <= STATUS_COMPACT_THRESHOLD) return;
|
|
166
|
+
const boundaryTs = entries[entries.length - STATUS_COMPACT_KEEP]!.ts;
|
|
167
|
+
const r = await archiveJsonl<StatusEntry>(
|
|
168
|
+
STATUS_FILE,
|
|
169
|
+
ARCHIVE_STATUS_FILE,
|
|
170
|
+
(e) => e.ts >= boundaryTs
|
|
171
|
+
);
|
|
172
|
+
if (r.removed > 0) await adjustCursors({ statusRemoved: r.removed });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------- read_messages ----------
|
|
176
|
+
|
|
177
|
+
export const readMessagesSchema = {
|
|
178
|
+
agentId: z.string().min(1),
|
|
179
|
+
source: z.enum(["inbox", "room", "status"]),
|
|
180
|
+
room: z.string().optional(),
|
|
181
|
+
limit: z.number().int().positive().max(500).optional(),
|
|
182
|
+
peek: z.boolean().optional(),
|
|
183
|
+
sinceTs: z.number().optional(),
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export async function readMessagesTool(args: {
|
|
187
|
+
agentId: string;
|
|
188
|
+
source: Source;
|
|
189
|
+
room?: string;
|
|
190
|
+
limit?: number;
|
|
191
|
+
peek?: boolean;
|
|
192
|
+
sinceTs?: number;
|
|
193
|
+
}) {
|
|
194
|
+
const file = sourceFile(args.source, args.agentId, args.room);
|
|
195
|
+
const all = await readJsonl<Message | StatusEntry>(file);
|
|
196
|
+
|
|
197
|
+
let entries: (Message | StatusEntry)[] = [];
|
|
198
|
+
let totalNew = 0;
|
|
199
|
+
|
|
200
|
+
// Room and status reads default to 50 entries to prevent agents flooding
|
|
201
|
+
// themselves with full history on join — the status stream grows unbounded
|
|
202
|
+
// across the fleet. Inbox drains fully since it is targeted by nature.
|
|
203
|
+
const effectiveLimit = args.limit ?? (args.source === "inbox" ? undefined : 50);
|
|
204
|
+
|
|
205
|
+
if (args.peek) {
|
|
206
|
+
const cursor = await readJson<Cursor>(cursorFile(args.agentId), {});
|
|
207
|
+
const startOffset = getOffset(cursor, args.source, args.room);
|
|
208
|
+
entries = all.slice(startOffset);
|
|
209
|
+
if (args.sinceTs !== undefined) entries = entries.filter((e) => e.ts > args.sinceTs!);
|
|
210
|
+
totalNew = entries.length;
|
|
211
|
+
} else {
|
|
212
|
+
await updateJson<Cursor>(cursorFile(args.agentId), {}, (current) => {
|
|
213
|
+
const startOffset = getOffset(current, args.source, args.room);
|
|
214
|
+
let e = all.slice(startOffset);
|
|
215
|
+
if (args.sinceTs !== undefined) e = e.filter((x) => x.ts > args.sinceTs!);
|
|
216
|
+
totalNew = e.length;
|
|
217
|
+
entries = e;
|
|
218
|
+
// Advance past EVERYTHING we account for here (recent window + any
|
|
219
|
+
// overflow we stash below). The overflow is recoverable via the history
|
|
220
|
+
// hash, so it must not requeue for the next read — that would re-flood.
|
|
221
|
+
if (e.length > 0) setOffset(current, args.source, args.room, startOffset + e.length);
|
|
222
|
+
return current;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// CCR overflow handling (room and status sources). When the backlog exceeds
|
|
227
|
+
// the window, return the RECENT slice raw and replace the older overflow with
|
|
228
|
+
// a compact digest carrying a retrieval hash. The agent expands it on demand
|
|
229
|
+
// via retrieve_room_history. Peek is side-effect-free, so it never stashes —
|
|
230
|
+
// it reports the count and tells the agent to do a real read to get a hash.
|
|
231
|
+
let recent = entries;
|
|
232
|
+
let history: { digest: string; hash?: string; older: number } | undefined;
|
|
233
|
+
if (args.source !== "inbox" && effectiveLimit && entries.length > effectiveLimit) {
|
|
234
|
+
const overflow = entries.slice(0, entries.length - effectiveLimit);
|
|
235
|
+
recent = entries.slice(entries.length - effectiveLimit);
|
|
236
|
+
const stashKey = args.source === "room" ? normalizeRoom(args.room) : "status";
|
|
237
|
+
if (args.peek) {
|
|
238
|
+
history = { digest: digestOverflow(overflow, undefined), older: overflow.length };
|
|
239
|
+
} else {
|
|
240
|
+
const hash = await stashHistory(stashKey, args.agentId, overflow);
|
|
241
|
+
history = { digest: digestOverflow(overflow, hash), hash, older: overflow.length };
|
|
242
|
+
}
|
|
243
|
+
} else if (effectiveLimit && entries.length > effectiveLimit) {
|
|
244
|
+
// Inbox keeps the legacy oldest-first chunking (no stash) — targeted
|
|
245
|
+
// messages must never be skipped over.
|
|
246
|
+
recent = entries.slice(0, effectiveLimit);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Drop the agent's own posts on shared channels — reading your own broadcast
|
|
250
|
+
// back is never useful and confuses turn-based agents into self-replies.
|
|
251
|
+
// Cursor has already advanced past them, so they won't reappear.
|
|
252
|
+
const visible =
|
|
253
|
+
args.source === "room" || args.source === "status"
|
|
254
|
+
? recent.filter((e) => entryAuthor(e) !== args.agentId)
|
|
255
|
+
: recent;
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
ok: true,
|
|
259
|
+
messages: visible,
|
|
260
|
+
totalNew,
|
|
261
|
+
returned: visible.length,
|
|
262
|
+
room: args.source === "room" ? normalizeRoom(args.room) : undefined,
|
|
263
|
+
...(history ? { history } : {}),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Lossless summary of a stashed backlog slice: surfaces error/failure posts
|
|
268
|
+
// verbatim (the lines that usually matter most in a flood) and collapses the
|
|
269
|
+
// rest to counts. Mirrors headroom's content-aware digest, kept deliberately
|
|
270
|
+
// simple — the full originals are one retrieve_room_history call away.
|
|
271
|
+
function digestOverflow(over: (Message | StatusEntry)[], hash: string | undefined): string {
|
|
272
|
+
const authors = new Set(over.map(entryAuthor).filter(Boolean));
|
|
273
|
+
const errorRe = /\b(error|fatal|fail(ed|ure)?|panic|exception)\b/i;
|
|
274
|
+
const errors = over.filter((m) => errorRe.test(JSON.stringify(m)));
|
|
275
|
+
const first = over[0]?.ts;
|
|
276
|
+
const last = over[over.length - 1]?.ts;
|
|
277
|
+
const span =
|
|
278
|
+
first && last && last > first ? ` over ${Math.round((last - first) / 60000)}m` : "";
|
|
279
|
+
const parts = [
|
|
280
|
+
`[${over.length} earlier message${over.length === 1 ? "" : "s"} compressed`,
|
|
281
|
+
`${authors.size} agent${authors.size === 1 ? "" : "s"}${span}`,
|
|
282
|
+
];
|
|
283
|
+
if (errors.length) parts.push(`${errors.length} error post${errors.length === 1 ? "" : "s"}`);
|
|
284
|
+
const head = parts.join(", ");
|
|
285
|
+
const tail = hash
|
|
286
|
+
? ` hash=${hash}] — call retrieve_room_history(hash="${hash}") to expand`
|
|
287
|
+
: `] — read without peek to get an expandable hash`;
|
|
288
|
+
// Decisions are the one thing a digest must not bury — quote them verbatim
|
|
289
|
+
// (capped) below the summary line.
|
|
290
|
+
const decisions = over.filter((m): m is Message => (m as Message).kind === "decision");
|
|
291
|
+
const quoted = decisions
|
|
292
|
+
.slice(-5)
|
|
293
|
+
.map((d) => ` [decision] ${d.from}: ${d.text.length > 200 ? d.text.slice(0, 200) + "…" : d.text}`);
|
|
294
|
+
const decisionBlock = decisions.length
|
|
295
|
+
? `\n${quoted.join("\n")}${decisions.length > 5 ? `\n (+${decisions.length - 5} earlier decisions in hash)` : ""}`
|
|
296
|
+
: "";
|
|
297
|
+
return head + tail + decisionBlock;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ---------- retrieve_room_history ----------
|
|
301
|
+
|
|
302
|
+
export const retrieveRoomHistorySchema = {
|
|
303
|
+
agentId: z.string().min(1),
|
|
304
|
+
hash: z.string().min(1),
|
|
305
|
+
query: z.string().optional(),
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
export async function retrieveRoomHistoryTool(args: {
|
|
309
|
+
agentId: string;
|
|
310
|
+
hash: string;
|
|
311
|
+
query?: string;
|
|
312
|
+
}) {
|
|
313
|
+
const res = await retrieveHistory<Message | StatusEntry>(args.hash, args.agentId, args.query);
|
|
314
|
+
if (!res.ok) {
|
|
315
|
+
const reason =
|
|
316
|
+
res.reason === "expired"
|
|
317
|
+
? "That history entry has expired (30m TTL). Re-read the channel with a higher limit to fetch it again."
|
|
318
|
+
: res.reason === "forbidden"
|
|
319
|
+
? "That history hash was produced for a different agent and cannot be retrieved by you."
|
|
320
|
+
: "No history entry for that hash. It may have expired or never existed.";
|
|
321
|
+
return { ok: false, reason: res.reason, message: reason };
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
ok: true,
|
|
325
|
+
room: res.room,
|
|
326
|
+
hash: args.hash,
|
|
327
|
+
total: res.total,
|
|
328
|
+
returned: res.messages.length,
|
|
329
|
+
messages: res.messages,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function entryAuthor(e: Message | StatusEntry): string | undefined {
|
|
334
|
+
return "from" in e ? e.from : e.agentId;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ---------- post_status ----------
|
|
338
|
+
|
|
339
|
+
export const postStatusSchema = {
|
|
340
|
+
agentId: z.string().min(1),
|
|
341
|
+
status: z.string().min(1),
|
|
342
|
+
detail: z.string().optional(),
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
export async function postStatusTool(args: { agentId: string; status: string; detail?: string }) {
|
|
346
|
+
const entry: StatusEntry = {
|
|
347
|
+
id: randomUUID(),
|
|
348
|
+
ts: Date.now(),
|
|
349
|
+
agentId: args.agentId,
|
|
350
|
+
status: args.status,
|
|
351
|
+
detail: args.detail,
|
|
352
|
+
};
|
|
353
|
+
await appendJsonl(STATUS_FILE, entry);
|
|
354
|
+
await maybeCompactStatus();
|
|
355
|
+
return { ok: true, id: entry.id };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// ---------- wait_for_message ----------
|
|
359
|
+
|
|
360
|
+
export const waitForMessageSchema = {
|
|
361
|
+
agentId: z.string().min(1),
|
|
362
|
+
source: z.enum(["inbox", "room", "status"]),
|
|
363
|
+
room: z.string().optional(),
|
|
364
|
+
timeoutMs: z.number().int().positive().max(MAX_WAIT_MS).optional(),
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export async function waitForMessageTool(args: {
|
|
368
|
+
agentId: string;
|
|
369
|
+
source: Source;
|
|
370
|
+
room?: string;
|
|
371
|
+
timeoutMs?: number;
|
|
372
|
+
}) {
|
|
373
|
+
const totalTimeout = Math.min(args.timeoutMs ?? 30_000, MAX_WAIT_MS);
|
|
374
|
+
const file = sourceFile(args.source, args.agentId, args.room);
|
|
375
|
+
const deadline = Date.now() + totalTimeout;
|
|
376
|
+
|
|
377
|
+
// Loop so that file growth caused only by the agent's own self-posts (which
|
|
378
|
+
// readMessagesTool now filters out for room/status) doesn't return an empty
|
|
379
|
+
// result — keep waiting until we have something to deliver or time out.
|
|
380
|
+
while (Date.now() < deadline) {
|
|
381
|
+
const startSize = await fileSize(file);
|
|
382
|
+
const remaining = deadline - Date.now();
|
|
383
|
+
if (remaining <= 0) break;
|
|
384
|
+
|
|
385
|
+
const changed = await new Promise<boolean>((resolve) => {
|
|
386
|
+
let settled = false;
|
|
387
|
+
const finish = (v: boolean) => {
|
|
388
|
+
if (settled) return;
|
|
389
|
+
settled = true;
|
|
390
|
+
clearInterval(poll);
|
|
391
|
+
try {
|
|
392
|
+
watcher?.close();
|
|
393
|
+
} catch {
|
|
394
|
+
// ignore
|
|
395
|
+
}
|
|
396
|
+
clearTimeout(t);
|
|
397
|
+
resolve(v);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const check = async () => {
|
|
401
|
+
const sz = await fileSize(file);
|
|
402
|
+
if (sz > startSize) finish(true);
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
let watcher: ReturnType<typeof watch> | undefined;
|
|
406
|
+
try {
|
|
407
|
+
watcher = watch(file, () => {
|
|
408
|
+
void check();
|
|
409
|
+
});
|
|
410
|
+
} catch {
|
|
411
|
+
// file may not exist; polling will handle
|
|
412
|
+
}
|
|
413
|
+
const poll = setInterval(() => void check(), 500);
|
|
414
|
+
const t = setTimeout(() => finish(false), remaining);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
if (!changed) break;
|
|
418
|
+
const result = await readMessagesTool({ agentId: args.agentId, source: args.source, room: args.room });
|
|
419
|
+
if (result.returned > 0) return result;
|
|
420
|
+
// otherwise, only self-posts arrived; keep waiting on the remaining budget
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return { ok: false, timedOut: true };
|
|
424
|
+
}
|
|
425
|
+
|