agent-coord-mcp 0.14.0 → 0.15.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.
@@ -0,0 +1,296 @@
1
+ import { z } from "zod";
2
+ import path from "node:path";
3
+ import { AGENTS_FILE, ARCHIVE_STATUS_FILE, CURSOR_DIR, DEFAULT_ROOM, INBOX_DIR, ROOMS_FILE, STATUS_FILE, archiveInboxFile, archiveJsonl, archiveRoomFile, deleteFile, getRooms, listCursorFiles, listInboxFiles, listReceiptFiles, normalizeRoom, pruneHistory, readJson, readJsonl, rewriteJsonl, roomFile, updateJson, } from "../store.js";
4
+ // ---------- prune ----------
5
+ export const PRUNE_TARGETS = ["rooms", "status", "inbox", "receipts", "members"];
6
+ export const pruneSchema = {
7
+ olderThanDays: z.number().positive().max(365).optional(),
8
+ decisionDays: z.number().positive().max(365).optional(),
9
+ room: z.string().optional(),
10
+ targets: z.array(z.enum(PRUNE_TARGETS)).optional(),
11
+ removeOrphanInboxes: z.boolean().optional(),
12
+ archiveEmptyRooms: z.boolean().optional(),
13
+ dryRun: z.boolean().optional(),
14
+ };
15
+ // Retention predicate: kind="decision" messages live by the (longer)
16
+ // decision cutoff; everything else by the standard cutoff.
17
+ function keepEntry(e, cutoff, decisionCutoff) {
18
+ return e.kind === "decision" ? e.ts > decisionCutoff : e.ts > cutoff;
19
+ }
20
+ // Shift every agent's cursor offsets down by the number of entries removed
21
+ // ahead of them (append-only + oldest-first removal ⇒ plain subtraction,
22
+ // clamped at 0). Shared by prune and live compaction — without this, an
23
+ // offset past the new file length makes read_messages return [] forever.
24
+ export async function adjustCursors(removals) {
25
+ const roomRemovedByChan = removals.roomRemovedByChan ?? {};
26
+ const statusRemoved = removals.statusRemoved ?? 0;
27
+ const perAgentInboxRemoved = removals.perAgentInboxRemoved ?? {};
28
+ const cursorsAdjusted = [];
29
+ for (const cname of await listCursorFiles()) {
30
+ const id = cname.replace(/\.json$/, "");
31
+ const cursorPath = path.join(CURSOR_DIR, cname);
32
+ let touched = false;
33
+ await updateJson(cursorPath, {}, (current) => {
34
+ for (const [chan, removed] of Object.entries(roomRemovedByChan)) {
35
+ if (chan === DEFAULT_ROOM) {
36
+ if (current.roomOffset !== undefined) {
37
+ current.roomOffset = Math.max(0, current.roomOffset - removed);
38
+ touched = true;
39
+ }
40
+ }
41
+ else if (current.roomOffsets?.[chan] !== undefined) {
42
+ current.roomOffsets[chan] = Math.max(0, current.roomOffsets[chan] - removed);
43
+ touched = true;
44
+ }
45
+ }
46
+ if (current.statusOffset !== undefined && statusRemoved > 0) {
47
+ current.statusOffset = Math.max(0, current.statusOffset - statusRemoved);
48
+ touched = true;
49
+ }
50
+ const myInboxRemoved = perAgentInboxRemoved[id] ?? 0;
51
+ if (current.inboxOffset !== undefined && myInboxRemoved > 0) {
52
+ current.inboxOffset = Math.max(0, current.inboxOffset - myInboxRemoved);
53
+ touched = true;
54
+ }
55
+ return current;
56
+ });
57
+ if (touched)
58
+ cursorsAdjusted.push(id);
59
+ }
60
+ return cursorsAdjusted;
61
+ }
62
+ export async function pruneTool(args) {
63
+ const days = args.olderThanDays ?? 7;
64
+ const decisionDays = args.decisionDays ?? 30;
65
+ const cutoff = Date.now() - days * 24 * 60 * 60 * 1000;
66
+ const decisionCutoff = Date.now() - decisionDays * 24 * 60 * 60 * 1000;
67
+ const dryRun = args.dryRun ?? false;
68
+ // room-scoped prune touches only that channel's messages; targets narrows
69
+ // the sweep otherwise. Default remains "everything".
70
+ const scopedRoom = args.room ? normalizeRoom(args.room) : undefined;
71
+ const targets = new Set(scopedRoom ? ["rooms"] : args.targets ?? [...PRUNE_TARGETS]);
72
+ const keep = (e) => keepEntry(e, cutoff, decisionCutoff);
73
+ const reg = await readJson(AGENTS_FILE, {});
74
+ const knownAgents = new Set(Object.keys(reg));
75
+ // A member is stale when its agent is gone from the registry (orphan) or
76
+ // hasn't heartbeated since the cutoff (dead session).
77
+ const staleAgent = (m) => !knownAgents.has(m) || (reg[m]?.lastHeartbeat ?? 0) <= cutoff;
78
+ const channels = scopedRoom ? [scopedRoom] : Object.keys(await getRooms());
79
+ if (dryRun) {
80
+ let roomMessages = 0;
81
+ if (targets.has("rooms")) {
82
+ for (const chan of channels) {
83
+ const msgs = await readJsonl(roomFile(chan));
84
+ roomMessages += msgs.filter((e) => !keep(e)).length;
85
+ }
86
+ }
87
+ const status = targets.has("status") ? await readJsonl(STATUS_FILE) : [];
88
+ let inboxRemoved = 0;
89
+ const orphans = [];
90
+ if (targets.has("inbox")) {
91
+ for (const fname of await listInboxFiles()) {
92
+ const id = fname.replace(/\.jsonl$/, "");
93
+ if (!knownAgents.has(id) && (args.removeOrphanInboxes ?? true))
94
+ orphans.push(fname);
95
+ const entries = await readJsonl(path.join(INBOX_DIR, fname));
96
+ inboxRemoved += entries.filter((e) => e.ts <= cutoff).length;
97
+ }
98
+ }
99
+ const orphanMembers = new Set();
100
+ const staleMembers = new Set();
101
+ const emptyRooms = [];
102
+ if (targets.has("members")) {
103
+ const rooms = await getRooms();
104
+ for (const [chan, e] of Object.entries(rooms)) {
105
+ const remaining = [];
106
+ for (const m of e.members ?? []) {
107
+ if (!knownAgents.has(m))
108
+ orphanMembers.add(m);
109
+ else if (staleAgent(m))
110
+ staleMembers.add(m);
111
+ else
112
+ remaining.push(m);
113
+ }
114
+ if ((args.archiveEmptyRooms ?? true) &&
115
+ chan !== DEFAULT_ROOM &&
116
+ remaining.length === 0) {
117
+ const msgs = await readJsonl(roomFile(chan));
118
+ const lastTs = msgs[msgs.length - 1]?.ts ?? 0;
119
+ if (lastTs <= cutoff)
120
+ emptyRooms.push(chan);
121
+ }
122
+ }
123
+ }
124
+ let receiptsRemoved = 0;
125
+ const orphanReceipts = [];
126
+ if (targets.has("receipts")) {
127
+ for (const filePath of await listReceiptFiles()) {
128
+ const id = path.basename(filePath).replace(/\.jsonl$/, "");
129
+ const entries = await readJsonl(filePath);
130
+ if (!knownAgents.has(id) && (args.removeOrphanInboxes ?? true))
131
+ orphanReceipts.push(id);
132
+ receiptsRemoved += entries.filter((e) => e.ts <= cutoff).length;
133
+ }
134
+ }
135
+ return {
136
+ dryRun: true,
137
+ cutoff,
138
+ olderThanDays: days,
139
+ decisionDays,
140
+ ...(scopedRoom ? { room: scopedRoom } : {}),
141
+ wouldRemove: {
142
+ roomMessages,
143
+ statusEntries: status.filter((e) => e.ts <= cutoff).length,
144
+ inboxMessages: inboxRemoved,
145
+ orphanInboxes: orphans,
146
+ orphanMembers: [...orphanMembers],
147
+ staleMembers: [...staleMembers],
148
+ archivedRooms: emptyRooms,
149
+ receipts: receiptsRemoved,
150
+ orphanReceipts,
151
+ },
152
+ };
153
+ }
154
+ // Per-channel removed counts so we can shift the matching offset in each
155
+ // cursor (default channel → roomOffset, others → roomOffsets[chan]).
156
+ // Aged-out entries are archived to archive/rooms/<chan>.jsonl, never deleted.
157
+ const roomRemovedByChan = {};
158
+ let roomRemovedTotal = 0;
159
+ if (targets.has("rooms")) {
160
+ for (const chan of channels) {
161
+ const r = await archiveJsonl(roomFile(chan), archiveRoomFile(chan), keep);
162
+ if (r.removed > 0) {
163
+ roomRemovedByChan[chan] = r.removed;
164
+ roomRemovedTotal += r.removed;
165
+ }
166
+ }
167
+ }
168
+ const statusResult = targets.has("status")
169
+ ? await archiveJsonl(STATUS_FILE, ARCHIVE_STATUS_FILE, (e) => e.ts > cutoff)
170
+ : { kept: 0, removed: 0, archived: 0 };
171
+ let inboxRemoved = 0;
172
+ const deletedOrphans = [];
173
+ // perAgentInboxRemoved: how many entries were stripped from each *kept* agent's
174
+ // inbox, so we can shift only that agent's inboxOffset cursor below.
175
+ const perAgentInboxRemoved = {};
176
+ if (targets.has("inbox")) {
177
+ for (const fname of await listInboxFiles()) {
178
+ const id = fname.replace(/\.jsonl$/, "");
179
+ const filePath = path.join(INBOX_DIR, fname);
180
+ if (!knownAgents.has(id) && (args.removeOrphanInboxes ?? true)) {
181
+ // Orphan inbox: archive everything it held, then drop the live file.
182
+ const r = await archiveJsonl(filePath, archiveInboxFile(id), () => false);
183
+ inboxRemoved += r.removed;
184
+ await deleteFile(filePath);
185
+ deletedOrphans.push(id);
186
+ continue;
187
+ }
188
+ const r = await archiveJsonl(filePath, archiveInboxFile(id), (e) => e.ts > cutoff);
189
+ inboxRemoved += r.removed;
190
+ perAgentInboxRemoved[id] = r.removed;
191
+ }
192
+ }
193
+ const cursorsAdjusted = await adjustCursors({
194
+ roomRemovedByChan,
195
+ statusRemoved: statusResult.removed,
196
+ perAgentInboxRemoved,
197
+ });
198
+ // Membership sweep: drop orphans (not in registry) and stale members (no
199
+ // heartbeat since cutoff) so list_rooms / joinedRooms() stop surfacing
200
+ // ghosts. Then archive non-default rooms left with no members and no
201
+ // activity since the cutoff: remaining messages move to the archive, the
202
+ // room leaves the registry, and every cursor forgets its offset — the same
203
+ // cleanup delete_room performs, minus the data loss.
204
+ const orphanMembers = new Set();
205
+ const staleMembers = new Set();
206
+ const archivedRooms = [];
207
+ if (targets.has("members")) {
208
+ await updateJson(ROOMS_FILE, {}, (current) => {
209
+ for (const e of Object.values(current)) {
210
+ if ((e.members?.length ?? 0) === 0)
211
+ continue;
212
+ e.members = (e.members ?? []).filter((m) => {
213
+ if (!knownAgents.has(m)) {
214
+ orphanMembers.add(m);
215
+ return false;
216
+ }
217
+ if (staleAgent(m)) {
218
+ staleMembers.add(m);
219
+ return false;
220
+ }
221
+ return true;
222
+ });
223
+ }
224
+ return current;
225
+ });
226
+ if (args.archiveEmptyRooms ?? true) {
227
+ const rooms = await getRooms();
228
+ for (const [chan, e] of Object.entries(rooms)) {
229
+ if (chan === DEFAULT_ROOM || (e.members?.length ?? 0) > 0)
230
+ continue;
231
+ const file = roomFile(chan);
232
+ const msgs = await readJsonl(file);
233
+ const lastTs = msgs[msgs.length - 1]?.ts ?? 0;
234
+ if (lastTs > cutoff)
235
+ continue;
236
+ await archiveJsonl(file, archiveRoomFile(chan), () => false);
237
+ await deleteFile(file);
238
+ await updateJson(ROOMS_FILE, {}, (current) => {
239
+ delete current[chan];
240
+ return current;
241
+ });
242
+ for (const cname of await listCursorFiles()) {
243
+ await updateJson(path.join(CURSOR_DIR, cname), {}, (current) => {
244
+ if (current.roomOffsets?.[chan] !== undefined)
245
+ delete current.roomOffsets[chan];
246
+ return current;
247
+ });
248
+ }
249
+ archivedRooms.push(chan);
250
+ }
251
+ }
252
+ }
253
+ // Receipts are out-of-band proof logs with no cursor and no analysis value —
254
+ // trim old entries by ts and delete logs for agents no longer registered.
255
+ // The one stream that is genuinely deleted, not archived.
256
+ let receiptsRemoved = 0;
257
+ const deletedOrphanReceipts = [];
258
+ if (targets.has("receipts")) {
259
+ for (const filePath of await listReceiptFiles()) {
260
+ const id = path.basename(filePath).replace(/\.jsonl$/, "");
261
+ if (!knownAgents.has(id) && (args.removeOrphanInboxes ?? true)) {
262
+ const entries = await readJsonl(filePath);
263
+ receiptsRemoved += entries.length;
264
+ await deleteFile(filePath);
265
+ deletedOrphanReceipts.push(id);
266
+ continue;
267
+ }
268
+ const r = await rewriteJsonl(filePath, (e) => e.ts > cutoff);
269
+ receiptsRemoved += r.removed;
270
+ }
271
+ }
272
+ // Sweep expired reversible-history entries (TTL'd cache; also self-prunes on
273
+ // every read, so this just catches entries on a server with few reads).
274
+ await pruneHistory();
275
+ return {
276
+ dryRun: false,
277
+ cutoff,
278
+ olderThanDays: days,
279
+ decisionDays,
280
+ ...(scopedRoom ? { room: scopedRoom } : {}),
281
+ removed: {
282
+ roomMessages: roomRemovedTotal,
283
+ statusEntries: statusResult.removed,
284
+ inboxMessages: inboxRemoved,
285
+ orphanInboxes: deletedOrphans,
286
+ orphanMembers: [...orphanMembers],
287
+ staleMembers: [...staleMembers],
288
+ archivedRooms,
289
+ receipts: receiptsRemoved,
290
+ orphanReceipts: deletedOrphanReceipts,
291
+ },
292
+ archivedTo: "archive/ (rooms/<chan>.jsonl, status.jsonl, inbox/<agent>.jsonl; receipts deleted)",
293
+ cursorsAdjusted,
294
+ };
295
+ }
296
+ //# sourceMapp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eAAe,IAAI,OAAO,CAAC,MAAM,CAAC;gBAClC,MAAM,UAAU,CAAC,QAAQ,CAAC,CAAC;gBAC3B,qBAAqB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC/B,SAAS;YACX,CAAC;YACD,MAAM,CAAC,GAAG,MAAM,YAAY,CAAiB,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC;YAC7E,eAAe,IAAI,CAAC,CAAC,OAAO,CAAC;QAC/B,CAAC;IACH,CAAC;IAED,6EAA6E;IAC7E,wEAAwE;IACxE,MAAM,YAAY,EAAE,CAAC;IAErB,OAAO;QACL,MAAM,EAAE,KAAK;QACb,MAAM;QACN,aAAa,EAAE,IAAI;QACnB,YAAY;QACZ,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3C,OAAO,EAAE;YACP,YAAY,EAAE,gBAAgB;YAC9B,aAAa,EAAE,YAAY,CAAC,OAAO;YACnC,aAAa,EAAE,YAAY;YAC3B,aAAa,EAAE,cAAc;YAC7B,aAAa,EAAE,CAAC,GAAG,aAAa,CAAC;YACjC,YAAY,EAAE,CAAC,GAAG,YAAY,CAAC;YAC/B,aAAa;YACb,QAAQ,EAAE,eAAe;YACzB,cAAc,EAAE,qBAAqB;SACtC;QACD,UAAU,EAAE,oFAAoF;QAChG,eAAe;KAChB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,7 @@
1
+ export * from "./shared.js";
2
+ export * from "./registry.js";
3
+ export * from "./rooms.js";
4
+ export * from "./messaging.js";
5
+ export * from "./transport.js";
6
+ export * from "./admin.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,317 @@
1
+ import { adjustCursors } from "./admin.js";
2
+ import { ARCHIVE_STATUS_FILE, archiveJsonl, archiveRoomFile } from "../store.js";
3
+ import { randomUUID } from "node:crypto";
4
+ import { watch } from "node:fs";
5
+ import { z } from "zod";
6
+ import { AGENTS_FILE, DEFAULT_ROOM, STATUS_FILE, appendJsonl, cursorFile, ensureRoom, fileSize, inboxFile, normalizeRoom, readJson, readJsonl, roomFile, stashHistory, retrieveHistory, updateJson, } from "../store.js";
7
+ import { sourceFile, getOffset, setOffset, MAX_WAIT_MS, } from "./shared.js";
8
+ // ---------- send_message ----------
9
+ export const sendMessageSchema = {
10
+ from: z.string().min(1),
11
+ to: z.string().optional(),
12
+ room: z.string().optional(),
13
+ text: z.string().min(1),
14
+ kind: z.enum(["decision", "status", "chatter"]).optional(),
15
+ };
16
+ export async function sendMessageTool(args) {
17
+ // DM → inbox. Otherwise resolve the channel (default `general`), make sure it
18
+ // exists in the registry, and tag the message with its channel.
19
+ if (args.to) {
20
+ const msg = {
21
+ id: randomUUID(),
22
+ ts: Date.now(),
23
+ from: args.from,
24
+ to: args.to,
25
+ text: args.text,
26
+ };
27
+ const target = inboxFile(args.to);
28
+ await appendJsonl(target, msg);
29
+ // Offline delivery is intentional (the inbox is created on demand), but a
30
+ // typo'd recipient shouldn't vanish silently — surface a warning when the
31
+ // target isn't a known agent so the caller can catch the mistake.
32
+ const reg = await readJson(AGENTS_FILE, {});
33
+ const warning = reg[args.to]
34
+ ? undefined
35
+ : `recipient '${args.to}' is not a registered agent — message stored in their inbox but no one may be listening`;
36
+ return { ok: true, id: msg.id, target, room: undefined, warning };
37
+ }
38
+ const chan = normalizeRoom(args.room);
39
+ if (chan !== DEFAULT_ROOM)
40
+ await ensureRoom(chan, args.from);
41
+ const msg = {
42
+ id: randomUUID(),
43
+ ts: Date.now(),
44
+ from: args.from,
45
+ room: chan,
46
+ text: args.text,
47
+ ...(args.kind ? { kind: args.kind } : {}),
48
+ };
49
+ const target = roomFile(chan);
50
+ await appendJsonl(target, msg);
51
+ await maybeCompactRoom(chan);
52
+ return { ok: true, id: msg.id, target, room: chan };
53
+ }
54
+ // ---------- live compaction (self-limiting streams) ----------
55
+ // Rooms and the status stream compact themselves on write: once a live file
56
+ // grows past its threshold, the oldest entries move to the archive (never
57
+ // deleted) and every cursor shifts down. Fresh decisions (< decisionDays old)
58
+ // are exempt — they stay in the live file. Cursor adjustment subtracts the
59
+ // full removed count, so an agent parked behind a kept decision may re-read
60
+ // it once; at-least-once beats silently skipping.
61
+ function envInt(name, fallback) {
62
+ const n = Number(process.env[name]);
63
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
64
+ }
65
+ const ROOM_COMPACT_THRESHOLD = envInt("AGENT_COORD_ROOM_MAX", 1000);
66
+ const ROOM_COMPACT_KEEP = envInt("AGENT_COORD_ROOM_KEEP", 500);
67
+ const STATUS_COMPACT_THRESHOLD = envInt("AGENT_COORD_STATUS_MAX", 2000);
68
+ const STATUS_COMPACT_KEEP = envInt("AGENT_COORD_STATUS_KEEP", 1000);
69
+ // Skip the entry count entirely while the file is small — the common case on
70
+ // every send. ~150 bytes/entry means the threshold can't be hit below this.
71
+ const COMPACT_SIZE_GATE = 100 * 1024;
72
+ const DECISION_FRESH_MS = 30 * 24 * 60 * 60 * 1000;
73
+ async function maybeCompactRoom(chan) {
74
+ const file = roomFile(chan);
75
+ if ((await fileSize(file)) < COMPACT_SIZE_GATE)
76
+ return;
77
+ const entries = await readJsonl(file);
78
+ if (entries.length <= ROOM_COMPACT_THRESHOLD)
79
+ return;
80
+ const boundaryTs = entries[entries.length - ROOM_COMPACT_KEEP].ts;
81
+ const decisionCutoff = Date.now() - DECISION_FRESH_MS;
82
+ const r = await archiveJsonl(file, archiveRoomFile(chan), (e) => e.ts >= boundaryTs || (e.kind === "decision" && e.ts > decisionCutoff));
83
+ if (r.removed > 0)
84
+ await adjustCursors({ roomRemovedByChan: { [chan]: r.removed } });
85
+ }
86
+ async function maybeCompactStatus() {
87
+ if ((await fileSize(STATUS_FILE)) < COMPACT_SIZE_GATE)
88
+ return;
89
+ const entries = await readJsonl(STATUS_FILE);
90
+ if (entries.length <= STATUS_COMPACT_THRESHOLD)
91
+ return;
92
+ const boundaryTs = entries[entries.length - STATUS_COMPACT_KEEP].ts;
93
+ const r = await archiveJsonl(STATUS_FILE, ARCHIVE_STATUS_FILE, (e) => e.ts >= boundaryTs);
94
+ if (r.removed > 0)
95
+ await adjustCursors({ statusRemoved: r.removed });
96
+ }
97
+ // ---------- read_messages ----------
98
+ export const readMessagesSchema = {
99
+ agentId: z.string().min(1),
100
+ source: z.enum(["inbox", "room", "status"]),
101
+ room: z.string().optional(),
102
+ limit: z.number().int().positive().max(500).optional(),
103
+ peek: z.boolean().optional(),
104
+ sinceTs: z.number().optional(),
105
+ };
106
+ export async function readMessagesTool(args) {
107
+ const file = sourceFile(args.source, args.agentId, args.room);
108
+ const all = await readJsonl(file);
109
+ let entries = [];
110
+ let totalNew = 0;
111
+ // Room and status reads default to 50 entries to prevent agents flooding
112
+ // themselves with full history on join — the status stream grows unbounded
113
+ // across the fleet. Inbox drains fully since it is targeted by nature.
114
+ const effectiveLimit = args.limit ?? (args.source === "inbox" ? undefined : 50);
115
+ if (args.peek) {
116
+ const cursor = await readJson(cursorFile(args.agentId), {});
117
+ const startOffset = getOffset(cursor, args.source, args.room);
118
+ entries = all.slice(startOffset);
119
+ if (args.sinceTs !== undefined)
120
+ entries = entries.filter((e) => e.ts > args.sinceTs);
121
+ totalNew = entries.length;
122
+ }
123
+ else {
124
+ await updateJson(cursorFile(args.agentId), {}, (current) => {
125
+ const startOffset = getOffset(current, args.source, args.room);
126
+ let e = all.slice(startOffset);
127
+ if (args.sinceTs !== undefined)
128
+ e = e.filter((x) => x.ts > args.sinceTs);
129
+ totalNew = e.length;
130
+ entries = e;
131
+ // Advance past EVERYTHING we account for here (recent window + any
132
+ // overflow we stash below). The overflow is recoverable via the history
133
+ // hash, so it must not requeue for the next read — that would re-flood.
134
+ if (e.length > 0)
135
+ setOffset(current, args.source, args.room, startOffset + e.length);
136
+ return current;
137
+ });
138
+ }
139
+ // CCR overflow handling (room and status sources). When the backlog exceeds
140
+ // the window, return the RECENT slice raw and replace the older overflow with
141
+ // a compact digest carrying a retrieval hash. The agent expands it on demand
142
+ // via retrieve_room_history. Peek is side-effect-free, so it never stashes —
143
+ // it reports the count and tells the agent to do a real read to get a hash.
144
+ let recent = entries;
145
+ let history;
146
+ if (args.source !== "inbox" && effectiveLimit && entries.length > effectiveLimit) {
147
+ const overflow = entries.slice(0, entries.length - effectiveLimit);
148
+ recent = entries.slice(entries.length - effectiveLimit);
149
+ const stashKey = args.source === "room" ? normalizeRoom(args.room) : "status";
150
+ if (args.peek) {
151
+ history = { digest: digestOverflow(overflow, undefined), older: overflow.length };
152
+ }
153
+ else {
154
+ const hash = await stashHistory(stashKey, args.agentId, overflow);
155
+ history = { digest: digestOverflow(overflow, hash), hash, older: overflow.length };
156
+ }
157
+ }
158
+ else if (effectiveLimit && entries.length > effectiveLimit) {
159
+ // Inbox keeps the legacy oldest-first chunking (no stash) — targeted
160
+ // messages must never be skipped over.
161
+ recent = entries.slice(0, effectiveLimit);
162
+ }
163
+ // Drop the agent's own posts on shared channels — reading your own broadcast
164
+ // back is never useful and confuses turn-based agents into self-replies.
165
+ // Cursor has already advanced past them, so they won't reappear.
166
+ const visible = args.source === "room" || args.source === "status"
167
+ ? recent.filter((e) => entryAuthor(e) !== args.agentId)
168
+ : recent;
169
+ return {
170
+ ok: true,
171
+ messages: visible,
172
+ totalNew,
173
+ returned: visible.length,
174
+ room: args.source === "room" ? normalizeRoom(args.room) : undefined,
175
+ ...(history ? { history } : {}),
176
+ };
177
+ }
178
+ // Lossless summary of a stashed backlog slice: surfaces error/failure posts
179
+ // verbatim (the lines that usually matter most in a flood) and collapses the
180
+ // rest to counts. Mirrors headroom's content-aware digest, kept deliberately
181
+ // simple — the full originals are one retrieve_room_history call away.
182
+ function digestOverflow(over, hash) {
183
+ const authors = new Set(over.map(entryAuthor).filter(Boolean));
184
+ const errorRe = /\b(error|fatal|fail(ed|ure)?|panic|exception)\b/i;
185
+ const errors = over.filter((m) => errorRe.test(JSON.stringify(m)));
186
+ const first = over[0]?.ts;
187
+ const last = over[over.length - 1]?.ts;
188
+ const span = first && last && last > first ? ` over ${Math.round((last - first) / 60000)}m` : "";
189
+ const parts = [
190
+ `[${over.length} earlier message${over.length === 1 ? "" : "s"} compressed`,
191
+ `${authors.size} agent${authors.size === 1 ? "" : "s"}${span}`,
192
+ ];
193
+ if (errors.length)
194
+ parts.push(`${errors.length} error post${errors.length === 1 ? "" : "s"}`);
195
+ const head = parts.join(", ");
196
+ const tail = hash
197
+ ? ` hash=${hash}] — call retrieve_room_history(hash="${hash}") to expand`
198
+ : `] — read without peek to get an expandable hash`;
199
+ // Decisions are the one thing a digest must not bury — quote them verbatim
200
+ // (capped) below the summary line.
201
+ const decisions = over.filter((m) => m.kind === "decision");
202
+ const quoted = decisions
203
+ .slice(-5)
204
+ .map((d) => ` [decision] ${d.from}: ${d.text.length > 200 ? d.text.slice(0, 200) + "…" : d.text}`);
205
+ const decisionBlock = decisions.length
206
+ ? `\n${quoted.join("\n")}${decisions.length > 5 ? `\n (+${decisions.length - 5} earlier decisions in hash)` : ""}`
207
+ : "";
208
+ return head + tail + decisionBlock;
209
+ }
210
+ // ---------- retrieve_room_history ----------
211
+ export const retrieveRoomHistorySchema = {
212
+ agentId: z.string().min(1),
213
+ hash: z.string().min(1),
214
+ query: z.string().optional(),
215
+ };
216
+ export async function retrieveRoomHistoryTool(args) {
217
+ const res = await retrieveHistory(args.hash, args.agentId, args.query);
218
+ if (!res.ok) {
219
+ const reason = res.reason === "expired"
220
+ ? "That history entry has expired (30m TTL). Re-read the channel with a higher limit to fetch it again."
221
+ : res.reason === "forbidden"
222
+ ? "That history hash was produced for a different agent and cannot be retrieved by you."
223
+ : "No history entry for that hash. It may have expired or never existed.";
224
+ return { ok: false, reason: res.reason, message: reason };
225
+ }
226
+ return {
227
+ ok: true,
228
+ room: res.room,
229
+ hash: args.hash,
230
+ total: res.total,
231
+ returned: res.messages.length,
232
+ messages: res.messages,
233
+ };
234
+ }
235
+ function entryAuthor(e) {
236
+ return "from" in e ? e.from : e.agentId;
237
+ }
238
+ // ---------- post_status ----------
239
+ export const postStatusSchema = {
240
+ agentId: z.string().min(1),
241
+ status: z.string().min(1),
242
+ detail: z.string().optional(),
243
+ };
244
+ export async function postStatusTool(args) {
245
+ const entry = {
246
+ id: randomUUID(),
247
+ ts: Date.now(),
248
+ agentId: args.agentId,
249
+ status: args.status,
250
+ detail: args.detail,
251
+ };
252
+ await appendJsonl(STATUS_FILE, entry);
253
+ await maybeCompactStatus();
254
+ return { ok: true, id: entry.id };
255
+ }
256
+ // ---------- wait_for_message ----------
257
+ export const waitForMessageSchema = {
258
+ agentId: z.string().min(1),
259
+ source: z.enum(["inbox", "room", "status"]),
260
+ room: z.string().optional(),
261
+ timeoutMs: z.number().int().positive().max(MAX_WAIT_MS).optional(),
262
+ };
263
+ export async function waitForMessageTool(args) {
264
+ const totalTimeout = Math.min(args.timeoutMs ?? 30_000, MAX_WAIT_MS);
265
+ const file = sourceFile(args.source, args.agentId, args.room);
266
+ const deadline = Date.now() + totalTimeout;
267
+ // Loop so that file growth caused only by the agent's own self-posts (which
268
+ // readMessagesTool now filters out for room/status) doesn't return an empty
269
+ // result — keep waiting until we have something to deliver or time out.
270
+ while (Date.now() < deadline) {
271
+ const startSize = await fileSize(file);
272
+ const remaining = deadline - Date.now();
273
+ if (remaining <= 0)
274
+ break;
275
+ const changed = await new Promise((resolve) => {
276
+ let settled = false;
277
+ const finish = (v) => {
278
+ if (settled)
279
+ return;
280
+ settled = true;
281
+ clearInterval(poll);
282
+ try {
283
+ watcher?.close();
284
+ }
285
+ catch {
286
+ // ignore
287
+ }
288
+ clearTimeout(t);
289
+ resolve(v);
290
+ };
291
+ const check = async () => {
292
+ const sz = await fileSize(file);
293
+ if (sz > startSize)
294
+ finish(true);
295
+ };
296
+ let watcher;
297
+ try {
298
+ watcher = watch(file, () => {
299
+ void check();
300
+ });
301
+ }
302
+ catch {
303
+ // file may not exist; polling will handle
304
+ }
305
+ const poll = setInterval(() => void check(), 500);
306
+ const t = setTimeout(() => finish(false), remaining);
307
+ });
308
+ if (!changed)
309
+ break;
310
+ const result = await readMessagesTool({ agentId: args.agentId, source: args.source, room: args.room });
311
+ if (result.returned > 0)
312
+ return result;
313
+ // otherwise, only self-posts arrived; keep waiting on the remaining budget
314
+ }
315
+ return { ok: false, timedOut: true };
316
+ }
317
+ //# sourceMappingURL=messaging.js.map