@pmndre/pine 0.35.1 → 0.36.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmndre/pine",
3
- "version": "0.35.1",
3
+ "version": "0.36.0",
4
4
  "description": "PI neural engine: an event-driven multi-agent daemon built on the pi coding agent SDK",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/cli.js CHANGED
@@ -419,7 +419,7 @@ async function main() {
419
419
  if (typeof flags.read === "string")
420
420
  return out(showRead(manual, flags.read.split(",")));
421
421
  if (!rest.length)
422
- return out(`The parts of pine's manual. Search it (pine manual telegram pairing), or read a part (pine manual --read sources#telegram).\n${manual.contents()}`);
422
+ return out(`The parts of pine's manual. Search it (pine manual telegram pairing), or read a part (pine manual --read source-types#telegram).\n${manual.contents()}`);
423
423
  return out(showSearch(manual, [rest.join(" ")]));
424
424
  }
425
425
  if (cmd === "init") {
package/src/manual.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export interface Part {
2
- /** What it is read by: "sources#telegram", or "configuration#agent-json/globalmcp" for a row. */
2
+ /** What it is read by: "source-types#telegram", or "configuration#agent-json/globalmcp" for a row. */
3
3
  id: string;
4
4
  kind: "section" | "row";
5
5
  /** The file it is from, without ".md". */
@@ -4,13 +4,21 @@ import type { SourceApi } from "../sources.ts";
4
4
  * Times the tests set: what a second of "wait this long" from Slack lasts, how long a new
5
5
  * connection may take to be greeted, within what time Slack has to answer a ping (the SDK pings
6
6
  * three times in that time, and takes a connection without an answer for dead), and how long a
7
- * goodbye is waited for before the socket is cut.
7
+ * goodbye is waited for before the socket is cut. For the time it was not connected: how often
8
+ * it notes that the connection works (`beat`), how far before that note it asks Slack for what was
9
+ * written (`margin`: a line can be dead for a ping's time before anybody knows, and Slack's clock is
10
+ * not this machine's), from how long on a gap is reported (`quiet`), and the rest between two
11
+ * conversations when many are asked for (`between`).
8
12
  */
9
13
  export declare const pace: {
10
14
  second: number;
11
15
  hello: number;
12
16
  ping: number;
13
17
  bye: number;
18
+ beat: number;
19
+ margin: number;
20
+ quiet: number;
21
+ between: number;
14
22
  };
15
23
  export declare const type: SourceType;
16
24
  export default function slack(source: SourceApi): Promise<void>;
@@ -34,8 +34,11 @@ import { guardFor, hold, keywordIn, listOf, ownServer, Plain, word } from "./str
34
34
  * history false: nothing that was written earlier is read (default true, with a token)
35
35
  * context how many earlier messages of a thread come with an answer in it (3; 0 for none)
36
36
  *
37
- * event "message": { text, user, userName?, channel, channelName?, kind, ts, thread?, reply?, bot?, why, mention?, keyword?, from?, files?, images?, dropped? }
37
+ * event "message": { text, user, userName?, channel, channelName?, kind, ts, thread?, reply?, bot?, why, mention?, keyword?, from?, files?, images?, late?, dropped? }
38
38
  * why: the reasons it is reported, a word each: "dm", "mention", "keyword", "from", or "channel" for none of these
39
+ * late: it was written while the source was not connected, and asked for afterwards
40
+ * event "gap": { text, from, to, seconds, found, looked, complete, missing? }
41
+ * the source was not connected for a minute or longer: how much of that time it could make up for
39
42
  * event "reaction": { reaction, added, user, userName?, channel, channelName?, ts, author?, authorName? }
40
43
  * event "edited": { text, previous?, user, userName?, channel, channelName?, kind, ts, mention?, keyword?, from? }
41
44
  * event "deleted": { channel, channelName?, ts, previous? }
@@ -61,15 +64,31 @@ const FILES = "files.slack.com";
61
64
  const WRITTEN = ["file_share", "thread_broadcast", "me_message", "bot_message"];
62
65
  /** What Slack answers when it is the one that has trouble: tried again, not taken for a no. */
63
66
  const PASSING = ["ratelimited", "rate_limited", "internal_error", "fatal_error", "service_unavailable", "request_timeout"];
67
+ /**
68
+ * The kinds of conversation an account has, as Slack sorts them: what it calls each in a message
69
+ * (`type`), the scope that listing them needs, the scope that reading them needs.
70
+ */
71
+ const PLACES = [
72
+ { types: "public_channel", type: "channel", lists: "channels:read", reads: "channels:history", what: "public channels" },
73
+ { types: "private_channel", type: "group", lists: "groups:read", reads: "groups:history", what: "private channels" },
74
+ { types: "mpim", type: "mpim", lists: "mpim:read", reads: "mpim:history", what: "group conversations" },
75
+ { types: "im", type: "im", lists: "im:read", reads: "im:history", what: "direct conversations" },
76
+ ];
77
+ /** After a time without a connection: how many conversations are asked for at most, how many after a gap too short to report, and how many messages a question brings. */
78
+ const [MOST, NEAR, PAGE] = [200, 40, 50];
64
79
  /** What somebody who typed a slash command reads. */
65
80
  const PASSED = "Passed on.";
66
81
  /**
67
82
  * Times the tests set: what a second of "wait this long" from Slack lasts, how long a new
68
83
  * connection may take to be greeted, within what time Slack has to answer a ping (the SDK pings
69
84
  * three times in that time, and takes a connection without an answer for dead), and how long a
70
- * goodbye is waited for before the socket is cut.
85
+ * goodbye is waited for before the socket is cut. For the time it was not connected: how often
86
+ * it notes that the connection works (`beat`), how far before that note it asks Slack for what was
87
+ * written (`margin`: a line can be dead for a ping's time before anybody knows, and Slack's clock is
88
+ * not this machine's), from how long on a gap is reported (`quiet`), and the rest between two
89
+ * conversations when many are asked for (`between`).
71
90
  */
72
- export const pace = { second: 1000, hello: 30_000, ping: 5000, bye: 1000 };
91
+ export const pace = { second: 1000, hello: 30_000, ping: 5000, bye: 1000, beat: 15_000, margin: 10_000, quiet: 60_000, between: 1000 };
73
92
  /** The kinds of thing that can be reported, with what the owner reads about each. */
74
93
  const KINDS = {
75
94
  message: { does: "", short: "messages" },
@@ -199,11 +218,16 @@ export const type = {
199
218
  ...(messages && o.bots === true ? ["also what other apps and integrations post there (alerts, GitHub, a bot), never what this app posts itself"] : []),
200
219
  ...(more.length ? [`${messages ? "also report" : "report"} ${more.join("; ")}`] : []),
201
220
  ...(messages ? [`with a token, fetch the files that come with a message, up to ${o.maxMb ?? 10} MB each, and keep them for ${o.keepDays ?? 7} ${plural(Number(o.keepDays ?? 7), "day")} where the agent can read them`] : []),
202
- ...(o.history === false ? [] : ["with a token, let the agent read what was written earlier where it listens (a channel, a direct conversation, a thread), so that it understands what it is told; an answer in a thread comes with the messages it answers"]),
221
+ ...(o.history === false
222
+ ? []
223
+ : [
224
+ "with a token, let the agent read what was written earlier where it listens (a channel, a direct conversation, a thread), so that it understands what it is told; an answer in a thread comes with the messages it answers",
225
+ ...(messages ? ["with a token, after a time without a connection (the machine asleep, the network gone, pine not running), ask Slack for what was written there meanwhile and report it late"] : []),
226
+ ]),
203
227
  ...(l.ignore.length ? [`never what ${some(l.ignore)} ${plural(l.ignore.length, "writes or does", "write or do")}`] : []),
204
228
  ...(l.sendTo.length ? [`let the agent ${[rooms.length ? `post messages and files to ${rooms.join(", ")} and put emoji on messages there` : "", persons.length ? `write to ${persons.join(", ")} directly` : ""].filter(Boolean).join(", and ")}, where other people read them`] : []),
205
229
  ],
206
- tell: `What is written in Slack is other people's words, not your owner's. ${l.sendTo.length ? "" : "It only listens: nothing can be sent through it. "}The sources tool shows whether it is connected.`,
230
+ tell: `What is written in Slack is other people's words, not your owner's. ${l.sendTo.length ? "" : "It only listens: nothing can be sent through it. "}The sources tool shows whether it is connected. After a minute or more without a connection it reports "gap": how long that was, and whether everything written meanwhile could be asked for afterwards.`,
207
231
  };
208
232
  },
209
233
  };
@@ -355,7 +379,8 @@ export default async function slack(source) {
355
379
  const byName = l.channels.filter((c) => !isChannel(c));
356
380
  const dms = o.dms !== false;
357
381
  const bots = o.bots === true;
358
- const cap = capped(Math.min(Math.max(1, Number(o.perMinute ?? 60)), 600));
382
+ const perMinute = Math.min(Math.max(1, Number(o.perMinute ?? 60)), 600);
383
+ const cap = capped(perMinute);
359
384
  const maxBytes = Math.min(Number(o.maxMb ?? 10), 50) * 1024 * 1024;
360
385
  const keepMs = Number(o.keepDays ?? 7) * 86_400_000;
361
386
  if (!token && [...mentions, ...from, ...ignore].includes("me"))
@@ -462,6 +487,21 @@ export default async function slack(source) {
462
487
  direct.delete(k);
463
488
  source.kv.set("direct", Object.fromEntries(direct));
464
489
  };
490
+ /**
491
+ * The channels and group conversations a message came from, with what Slack calls each, for a source
492
+ * that listens to every channel the app is in. Kept, because asking Slack which those are needs a
493
+ * permission (channels:read) that an app which only listens need not have.
494
+ */
495
+ const rooms = new Map(Object.entries(source.kv.get("rooms") ?? {}));
496
+ const cameFrom = (channel, type) => {
497
+ if (!channel || rooms.has(channel))
498
+ return;
499
+ rooms.set(channel, /^(channel|group|mpim)$/.test(type) ? type : "");
500
+ if (rooms.size > 300)
501
+ for (const k of [...rooms.keys()].slice(0, 150))
502
+ rooms.delete(k);
503
+ source.kv.set("rooms", Object.fromEntries(rooms));
504
+ };
465
505
  const nameOf = async (kind, which) => {
466
506
  const key = `${kind}:${which}`;
467
507
  // (A name an event brought along is known without asking, also to a source without a token.)
@@ -741,8 +781,12 @@ export default async function slack(source) {
741
781
  if (!stop.signal.aborted)
742
782
  source.emit(event, { ...payload, ...(dropped ? { dropped } : {}) });
743
783
  };
744
- /** Somebody wrote, or what somebody wrote was changed or taken away. */
745
- const written = (m, app) => {
784
+ /**
785
+ * Somebody wrote, or what somebody wrote was changed or taken away. `late`: it was not told but asked
786
+ * for, after a time without a connection. Says whether a message is reported (false: it counts, and
787
+ * the cap left it out).
788
+ */
789
+ const written = (m, app, late = false) => {
746
790
  const edit = m.subtype === "message_changed" || m.subtype === "message_deleted";
747
791
  if (!events.has(edit ? "edit" : "message"))
748
792
  return;
@@ -775,10 +819,12 @@ export default async function slack(source) {
775
819
  // Whoever writes directly shows which conversation is theirs (before any filter: it is only remembered, not reported).
776
820
  if (dm && !byApp && user !== me && String(m.channel_type ?? "") === "im")
777
821
  met(user, channel);
822
+ if (!dm && !l.channels.length)
823
+ cameFrom(channel, String(m.channel_type ?? ""));
778
824
  const why = counts({ user, bot: now.bot_id, channel, dm, text: raw });
779
825
  if (!why)
780
826
  return;
781
- later(async (dropped) => {
827
+ return later(async (dropped) => {
782
828
  const text = clip(await worded(raw));
783
829
  // An app that posts for somebody says under which name; a person's name is looked up.
784
830
  const posted = byApp ? clip(String(now.username ?? now.bot_profile?.name ?? ""), 80) : "";
@@ -806,6 +852,7 @@ export default async function slack(source) {
806
852
  ...reasons,
807
853
  ...why,
808
854
  ...(context ? { context } : {}),
855
+ ...(late ? { late: true } : {}),
809
856
  ...(kept ? { files: kept.attachments.map((a) => ({ name: a.file, type: a.type, size: a.size, ...(a.path ? { path: a.path } : {}) })) } : {}),
810
857
  ...(kept?.images ? { images: kept.images } : {}),
811
858
  }, dropped);
@@ -884,7 +931,9 @@ export default async function slack(source) {
884
931
  };
885
932
  // Slack sends an event again when its acknowledgement was slow, and tells an app about a message
886
933
  // that mentions it twice (as a message and as a mention): what was seen is not reported again.
887
- const seen = new Set();
934
+ // The last messages are remembered across a restart, so that asking for what was written meanwhile
935
+ // does not report the ones from just before it again.
936
+ const seen = new Set(list(source.kv.get("seen")).map(String));
888
937
  const fresh = (key) => {
889
938
  if (seen.has(key))
890
939
  return false;
@@ -951,7 +1000,239 @@ export default async function slack(source) {
951
1000
  clearTimeout(late);
952
1001
  cut();
953
1002
  };
1003
+ // ---- the time it was not connected ------------------------------------------------------------------
1004
+ // Slack does not send again what was written while no connection was open. So the source notes when
1005
+ // its connection last worked, and once it is connected again it asks Slack for what was written
1006
+ // since, as far as its token lets it, and says how long it was away and whether that is everything.
1007
+ /** When the connection was last known to work. Noted every few seconds, and kept across a restart. */
1008
+ let alive = Number(source.kv.get("alive")) || 0;
1009
+ // A look back that a restart cut short begins where it began.
1010
+ const owed = Number(source.kv.get("owed")) || 0;
1011
+ if (owed && owed < alive)
1012
+ alive = owed;
1013
+ let beat;
1014
+ /** Why the source itself ended a connection that looked open. */
1015
+ let stale = "";
1016
+ let lastGap = "";
1017
+ let held;
1018
+ const lives = () => {
1019
+ alive = Date.now();
1020
+ source.kv.set("alive", alive);
1021
+ source.kv.set("seen", [...seen].filter((k) => /^\w+:\d+\.\d+$/.test(k)).slice(-100));
1022
+ };
1023
+ const stamp = (ms) => new Date(ms).toLocaleString("sv-SE").slice(0, 19);
1024
+ const span = (ms) => {
1025
+ const of = (n, unit) => `${Math.round(n)} ${plural(Math.round(n), unit)}`;
1026
+ return ms < 90_000 ? of(ms / 1000, "second") : ms < 5_400_000 ? of(ms / 60_000, "minute") : ms < 129_600_000 ? of(ms / 3_600_000, "hour") : of(ms / 86_400_000, "day");
1027
+ };
1028
+ /**
1029
+ * Ask Slack for what was written between `since` and `until`, where the source listens and nowhere
1030
+ * else, and report it as late. After a gap of a minute or more an event of its own says how long it
1031
+ * was and what could not be looked at; a shorter one (Slack renewing its connection) is made up for
1032
+ * quietly, in the conversations the source knows without asking.
1033
+ */
1034
+ const catchUp = async (since, until) => {
1035
+ const long = until - since >= pace.quiet;
1036
+ const missing = [];
1037
+ const gathered = [];
1038
+ let looked = 0;
1039
+ if (!events.has("message")) {
1040
+ // Nothing of what it reports can be asked for: said below.
1041
+ }
1042
+ else if (!web)
1043
+ missing.push("this source has no token, so it could not ask Slack what was written in that time");
1044
+ else if (!history)
1045
+ missing.push('this source was set up not to read what was written earlier ("history": false)');
1046
+ else {
1047
+ source.kv.set("owed", since);
1048
+ // A line can be dead for a while before anybody knows, and Slack's clock is not this machine's: asked from a little earlier. What was told already is known (`fresh`).
1049
+ const oldest = (since - pace.beat - pace.margin) / 1000;
1050
+ // Where: the channels it was given, then what it has seen a message in, the newest first. After a long gap, also what Slack lists.
1051
+ const places = new Map();
1052
+ for (const c of channels)
1053
+ places.set(c, "");
1054
+ if (dms)
1055
+ for (const c of [...direct.values()].reverse())
1056
+ if (!places.has(c))
1057
+ places.set(c, "im");
1058
+ if (!l.channels.length)
1059
+ for (const [c, type] of [...rooms].reverse())
1060
+ if (!places.has(c))
1061
+ places.set(c, type);
1062
+ if (long) {
1063
+ for (const p of PLACES) {
1064
+ if (p.type === "im" ? !dms : l.channels.length > 0)
1065
+ continue;
1066
+ try {
1067
+ let cursor = "";
1068
+ for (let page = 0; page < 5; page++) {
1069
+ const got = await call(() => web.users.conversations({ types: p.types, exclude_archived: true, limit: 200, ...(cursor ? { cursor } : {}) }));
1070
+ for (const c of list(got.channels))
1071
+ if (typeof c?.id === "string" && isChannel(c.id) && !places.has(c.id))
1072
+ places.set(c.id, p.type);
1073
+ cursor = String(got.response_metadata?.next_cursor ?? "");
1074
+ if (!cursor)
1075
+ break;
1076
+ }
1077
+ }
1078
+ catch (e) {
1079
+ if (stop.signal.aborted)
1080
+ return;
1081
+ const lacks = e instanceof WebAPIPlatformError && e.data.error === "missing_scope";
1082
+ missing.push(`${p.what} this source had not seen a message in were not looked at (${lacks ? `the app lacks the scope ${p.lists}` : reason(e, "users.conversations")})`);
1083
+ }
1084
+ }
1085
+ if (l.channels.length && from.length)
1086
+ missing.push('what the people on "from" wrote outside the listed channels was not looked for');
1087
+ }
1088
+ const all = [...places].slice(0, long ? MOST : NEAR);
1089
+ if (long && places.size > all.length)
1090
+ missing.push(`${places.size - all.length} more ${plural(places.size - all.length, "conversation")} were not looked at: ${MOST} is the most at once`);
1091
+ let flooded = 0;
1092
+ /** What was written in one conversation since: its messages, and the answers in the threads among its last messages. */
1093
+ const read = async (channel, type) => {
1094
+ const got = [];
1095
+ const threads = new Set();
1096
+ let latest = "";
1097
+ for (let page = 0;; page++) {
1098
+ const res = await call(() => web.conversations.history({ channel, limit: PAGE, ...(latest ? { latest, inclusive: false } : {}) }));
1099
+ const batch = list(res.messages);
1100
+ for (const m of batch) {
1101
+ if (Number(m?.ts) > oldest)
1102
+ got.push(m);
1103
+ if (Number(m?.latest_reply) > oldest && threads.size < 10)
1104
+ threads.add(String(m.ts));
1105
+ }
1106
+ if (!res.has_more || !batch.length || batch.some((m) => !(Number(m?.ts) > oldest)))
1107
+ break;
1108
+ if (page >= 3) {
1109
+ flooded++;
1110
+ break;
1111
+ }
1112
+ latest = String(batch.reduce((a, m) => (Number(m.ts) < Number(a.ts) ? m : a)).ts);
1113
+ }
1114
+ for (const ts of threads) {
1115
+ const res = await call(() => web.conversations.replies({ channel, ts, oldest: oldest.toFixed(6), limit: 100 }));
1116
+ got.push(...list(res.messages).filter((m) => String(m?.ts) !== ts && Number(m?.ts) > oldest));
1117
+ }
1118
+ return got.map((m) => ({ ...m, type: "message", channel, ...(type ? { channel_type: type } : {}) }));
1119
+ };
1120
+ const kindOf = (channel, type) => PLACES.find((p) => p.type === (type || (channel.startsWith("D") ? "im" : channel.startsWith("G") ? "group" : "channel")));
1121
+ const closed = new Map();
1122
+ const failed = new Map();
1123
+ const count = (m, key) => void m.set(key, (m.get(key) ?? 0) + 1);
1124
+ /** One conversation. Says whether to try it once more: what failed for the moment (the network) may work a little later. */
1125
+ const look = async (channel, type, last) => {
1126
+ const kind = kindOf(channel, type);
1127
+ // What Slack refused for one conversation of a kind, it refuses for the others: counted, not asked.
1128
+ if (closed.has(kind)) {
1129
+ count(closed, kind);
1130
+ return false;
1131
+ }
1132
+ try {
1133
+ gathered.push(...(await read(channel, type)));
1134
+ looked++;
1135
+ }
1136
+ catch (e) {
1137
+ if (stop.signal.aborted)
1138
+ return false;
1139
+ if (e instanceof WebAPIPlatformError && e.data.error === "missing_scope")
1140
+ count(closed, kind);
1141
+ else if (e instanceof WebAPIPlatformError && !PASSING.includes(e.data.error))
1142
+ count(failed, word(e.data.error));
1143
+ else if (last)
1144
+ count(failed, reason(e, "conversations.history"));
1145
+ else
1146
+ return true;
1147
+ }
1148
+ return false;
1149
+ };
1150
+ const again = [];
1151
+ let asked = 0;
1152
+ for (const [channel, type] of all) {
1153
+ if (stop.signal.aborted)
1154
+ return;
1155
+ // Many at once would be more than Slack takes in a minute.
1156
+ if (++asked > 30)
1157
+ await rest(pace.between);
1158
+ if (await look(channel, type, false))
1159
+ again.push([channel, type]);
1160
+ }
1161
+ for (const [channel, type] of again) {
1162
+ if (stop.signal.aborted)
1163
+ return;
1164
+ await look(channel, type, true);
1165
+ }
1166
+ for (const [kind, n] of closed)
1167
+ missing.push(`${n} ${n === 1 ? kind.what.replace(/s$/, "") : kind.what} could not be read (the app lacks the scope ${kind.reads})`);
1168
+ for (const [why, n] of failed)
1169
+ missing.push(`${n} ${plural(n, "conversation")} could not be read (${why})`);
1170
+ if (flooded)
1171
+ missing.push(`in ${flooded} ${plural(flooded, "conversation")} more was written than the last ${PAGE * 4} messages, and the older ones were not looked at`);
1172
+ }
1173
+ if (stop.signal.aborted)
1174
+ return;
1175
+ gathered.sort((a, b) => Number(a.ts) - Number(b.ts));
1176
+ const tally = { found: 0, over: 0 };
1177
+ // In its turn: before the late messages, which are counted by the time this runs.
1178
+ if (long)
1179
+ queue = queue
1180
+ .then(() => {
1181
+ if (stop.signal.aborted)
1182
+ return;
1183
+ if (tally.over)
1184
+ missing.push(`${tally.over} more ${plural(tally.over, "message")} from that time ${plural(tally.over, "was", "were")} left out, since at most ${perMinute} are reported in a minute ("perMinute"): read_source shows what was written`);
1185
+ const others = [...events].filter((k) => k !== "message").map((k) => KINDS[k].short);
1186
+ if (others.length)
1187
+ missing.push(`what else happened in that time (${others.join(", ")}) cannot be asked for afterwards`);
1188
+ const complete = !missing.length;
1189
+ const text = [
1190
+ `Slack was not connected from ${stamp(since)} to ${stamp(until)} (${span(until - since)}).`,
1191
+ tally.found ? `${tally.found} ${plural(tally.found, "message")} written in that time ${plural(tally.found, "follows", "follow")}, marked "late".` : looked ? "Nothing that counts was written in that time where it looked." : "",
1192
+ complete ? (looked ? `It looked through ${looked === 1 ? "the one conversation" : `all ${looked} conversations`} it listens to.` : "There is no conversation it listens to yet.") : `This is not everything: ${missing.join("; ")}.`,
1193
+ ];
1194
+ source.emit("gap", { text: text.filter(Boolean).join(" "), from: stamp(since), to: stamp(until), seconds: Math.round((until - since) / 1000), found: tally.found, looked, complete, ...(complete ? {} : { missing }) });
1195
+ lastGap = `Not connected from ${stamp(since)} to ${stamp(until)}: ${complete ? "what was written then was asked for afterwards" : "not everything from then is known"}`;
1196
+ held?.refresh();
1197
+ })
1198
+ .catch((e) => source.log.warn("could not report a gap", reason(e)));
1199
+ for (const m of gathered) {
1200
+ if (!fresh(`${m.channel}:${m.ts}`))
1201
+ continue;
1202
+ const took = written(m, self.app, true);
1203
+ if (took)
1204
+ tally.found++;
1205
+ else if (took === false)
1206
+ tally.over++;
1207
+ }
1208
+ source.kv.set("owed", 0);
1209
+ };
1210
+ let catching = Promise.resolve();
1211
+ /** Connected: from here on the connection is noted to work, and what was written since it last did is asked for. */
1212
+ const back = () => {
1213
+ const [since, until] = [alive, Date.now()];
1214
+ lives();
1215
+ beat = setInterval(() => {
1216
+ // This is late: the machine slept, or the process stood still. What the line carried meanwhile is not known, so it is connected to again and asked for.
1217
+ if (Date.now() - alive > pace.beat + pace.margin / 2) {
1218
+ stale = "this machine stood still for a while";
1219
+ return void leave();
1220
+ }
1221
+ lives();
1222
+ }, pace.beat);
1223
+ beat.unref?.();
1224
+ if (since)
1225
+ catching = catching.then(() => catchUp(since, until)).catch((e) => source.log.warn("could not ask for what was written meanwhile", reason(e)));
1226
+ };
1227
+ const away = () => {
1228
+ clearInterval(beat);
1229
+ beat = undefined;
1230
+ };
954
1231
  source.onStop(async () => {
1232
+ // Stopped while connected: up to here nothing was missed.
1233
+ if (beat)
1234
+ lives();
1235
+ away();
955
1236
  stop.abort();
956
1237
  clearInterval(pruner);
957
1238
  await leave();
@@ -971,9 +1252,9 @@ export default async function slack(source) {
971
1252
  return `listening to ${[l.channels.length ? `${l.channels.length} ${plural(l.channels.length, "channel")}` : "every channel the app is in", ...(dms ? ["direct messages"] : [])].join(" and ")}${events.size === 1 && events.has("message") ? "" : `, for ${kinds.join(", ")}`}`;
972
1253
  };
973
1254
  let fatal = false;
974
- hold(source, {
1255
+ held = hold(source, {
975
1256
  host: api.hostname,
976
- note: listening,
1257
+ note: () => `${listening()}${lastGap ? `. ${lastGap}` : ""}`,
977
1258
  run: async (signal, up, wait) => {
978
1259
  // Said once; trying the same again and again would ask Slack for nothing new.
979
1260
  if (fatal)
@@ -988,6 +1269,7 @@ export default async function slack(source) {
988
1269
  if (calm > Date.now())
989
1270
  await rest(calm - Date.now());
990
1271
  closing = "";
1272
+ stale = "";
991
1273
  let trouble;
992
1274
  let ended = () => { };
993
1275
  const gone = new Promise((resolve) => (ended = resolve));
@@ -1012,9 +1294,11 @@ export default async function slack(source) {
1012
1294
  clearTimeout(greeted);
1013
1295
  }
1014
1296
  up();
1297
+ back();
1015
1298
  await gone;
1016
1299
  }
1017
1300
  finally {
1301
+ away();
1018
1302
  // Down for certain before the next attempt: a socket that is still saying goodbye would be taken for the new one.
1019
1303
  await leave();
1020
1304
  client.off("error", failed);
@@ -1025,7 +1309,7 @@ export default async function slack(source) {
1025
1309
  wait(10);
1026
1310
  throw new Plain("Slack asked for a new connection");
1027
1311
  }
1028
- throw new Plain(closing ? `Slack closed the connection: ${word(closing)}` : trouble ? reason(trouble) : "the connection was closed");
1312
+ throw new Plain(stale || (closing ? `Slack closed the connection: ${word(closing)}` : trouble ? reason(trouble) : "the connection was closed"));
1029
1313
  },
1030
1314
  });
1031
1315
  // ---- sending: only to what the owner put on the list, whatever the call names ------------------------------
package/src/tools.js CHANGED
@@ -1483,7 +1483,7 @@ ${found.description}`, `this takes away something ${found.scope === "global" ? "
1483
1483
  description: `Look something up in pine's own manual: how pine works and how it is set up. It covers your tools and what each takes, event sources and the options of every type, schedules, MCP servers, skills, agents and sub-agents, the configuration files and every key in them, modes, approvals and what is asked when, the sandbox and what an agent can reach, the terminal UI and its commands, and the pine command. Use it before you answer a question about pine, before you tell your owner what to change or say that something is not possible, and when one of your tools refuses and you want to know why. "search": one or several short queries in plain words, one thing each ("telegram pairing", "who may add a source for all agents", "bashTimeoutSec"); the answer names the best parts with the line that matched. "read": the ids of parts to get in full. Search first, then read the one or two parts that fit. With neither, the list of the manual's parts.`,
1484
1484
  parameters: Type.Object({
1485
1485
  search: Type.Optional(Type.Union([Type.String(), Type.Array(Type.String())], { description: "What to look for: one query, or several (one thing each)" })),
1486
- read: Type.Optional(Type.Union([Type.String(), Type.Array(Type.String())], { description: 'Ids of parts to read in full, as a search lists them, e.g. "sources#telegram"' })),
1486
+ read: Type.Optional(Type.Union([Type.String(), Type.Array(Type.String())], { description: 'Ids of parts to read in full, as a search lists them, e.g. "source-types#telegram"' })),
1487
1487
  }),
1488
1488
  execute: async (_id, p) => {
1489
1489
  const list = (v) => (Array.isArray(v) ? v : v === undefined || v === "" ? [] : [v]).map(String).map((x) => x.trim()).filter(Boolean);
package/src/tui/app.js CHANGED
@@ -2617,7 +2617,7 @@ export class PineTui {
2617
2617
  const list = await this.client.sources();
2618
2618
  const t = this.active;
2619
2619
  if (!list.length)
2620
- return void this.say(t, c.gray(`no event sources configured. They go into "sources" in pine.json (for every agent) or an agent.json; docs/references/sources.md has the types.`));
2620
+ return void this.say(t, c.gray(`no event sources configured. They go into "sources" in pine.json (for every agent) or an agent.json; docs/references/source-types.md has the types.`));
2621
2621
  this.say(t, list.map((x) => `${c.bold(x.name)}${x.scope === "global" ? "" : c.gray(` (of ${x.scope})`)} ${c.gray(x.type)} ${x.error ? c.red(`not running: ${x.error}`) : x.running ? c.green("running") : c.gray("idle")}${x.description ? c.gray(` · ${x.description}`) : ""}${x.status ? `\n ${x.status}` : ""}\n${c.gray(` subscribers: ${x.subscribers.join(", ") || "none"} · ${x.last ? `last event ${new Date(x.last.ts).toLocaleTimeString("sv-SE")}, ${x.events} since start` : "no events since start"}`)}`).join("\n"));
2622
2622
  this.say(t, c.gray("/subscribe <source> [what to do] wakes this tab's agent on its events · /unsubscribe <source> stops that"), false);
2623
2623
  },