shadok-ai 0.20.18 → 0.20.21

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,89 @@
1
+ /**
2
+ * What a reconnecting Telegram bridge missed, and what it is allowed to post.
3
+ *
4
+ * The two halves of the mirror did not have the same safety net, and the gap
5
+ * between them is what a live instance reported: the agent's answers kept
6
+ * landing in the topic while the operator's own web messages silently never
7
+ * did, intermittently, for about ten minutes.
8
+ *
9
+ * The asymmetry is structural, not a flap.
10
+ *
11
+ * - The agent's text lives in the transcript, and the tail persists its byte
12
+ * offset (`writePos`, invariant 7), so a gap is re-read and re-broadcast.
13
+ * - A `prompt-echo` is ONE live frame, broadcast at submit time. It is derived
14
+ * from nothing on disk, so if no bridge is attached at that instant it is
15
+ * gone — with no trace anywhere, since `broadcast` writes to a client set
16
+ * and a set that lacks the bridge looks exactly like one that has it.
17
+ *
18
+ * And a reattaching bridge deliberately replays NOTHING (`backfill` is set only
19
+ * for a freshly created topic, "never on a reconnect/reboot") — which is right
20
+ * against re-spamming the whole history on every auto-update, and is why the
21
+ * one thing that could have recovered the frame did not.
22
+ *
23
+ * So the bridge gets the tail's cure, in the tail's shape: a resume point per
24
+ * binding, and a reconnect that posts the gap and only the gap.
25
+ *
26
+ * Pure so the rule is unit-testable; the store is in `src/channels.ts` (beside
27
+ * the other per-launch-dir Telegram state) and the wiring in `src/telegram.ts`.
28
+ */
29
+ /** At most this many missed messages are posted on a reconnect. A gap can be a
30
+ * weekend long, and a topic that answers a reattach with two hundred messages
31
+ * is not a recovery, it is a second incident. What is older is COUNTED, so the
32
+ * line that says it is replaying also says what it gave up on. */
33
+ export const REPLAY_CAP = 20;
34
+ /**
35
+ * Only these two are replayed. `bash` and `social-draft` are web-side cards
36
+ * with no Telegram rendering at all — but a `file` turn is a STATED limit, not
37
+ * an omission: a file delivered during the gap is not re-uploaded. Replaying it
38
+ * means reading bytes off disk with the agent's rights, long after the fact, for
39
+ * a path that may be gone; the text around it comes back and says there was one.
40
+ */
41
+ const MIRRORED_ROLES = new Set(["user", "assistant"]);
42
+ /**
43
+ * The messages a reattaching bridge should post, given what it had already
44
+ * delivered.
45
+ *
46
+ * Three refusals carry the design:
47
+ *
48
+ * - **No mark at all replays nothing.** A binding we have never delivered
49
+ * through is a NEW mirror, not one with an unbounded backlog — the same rule
50
+ * as the ledger's watermark, where no record means a fresh agent anchored to
51
+ * now rather than the whole table replayed into it.
52
+ * - **A turn with no `at` is never posted.** An older transcript has no
53
+ * timestamp, so the turn cannot be placed against the mark; posting it would
54
+ * re-spam history on every reconnect, which is the exact thing `backfill`
55
+ * exists to prevent.
56
+ * - **The mark's own echo at the head is dropped.** Same trade-off as
57
+ * `isStalePreface`: a duplicate in a topic is permanent, nothing ever comes
58
+ * to edit it away, while dropping this one costs at most the sentence the
59
+ * operator just typed and can still see in front of them.
60
+ */
61
+ export function replayPlan(turns, mark, cap = REPLAY_CAP) {
62
+ if (!mark)
63
+ return { post: [], dropped: 0 };
64
+ const after = turns.filter((t) => MIRRORED_ROLES.has(t.role) && typeof t.at === "number" && t.at > mark.at && typeof t.text === "string" && !!t.text.trim());
65
+ // Only at the HEAD: the same sentence sent again later is a real message and
66
+ // must survive. Dropping every match would make a repeated prompt vanish.
67
+ const head = after[0];
68
+ const body = head && head.role === "user" && mark.text && head.text.trim() === mark.text.trim() ? after.slice(1) : after;
69
+ const post = cap >= 0 ? body.slice(body.length - Math.min(cap, body.length)) : body;
70
+ return { post, dropped: body.length - post.length };
71
+ }
72
+ /**
73
+ * Move a binding's resume point forward — and only ever forward.
74
+ *
75
+ * The two clocks that feed it are different sources (a transcript timestamp and
76
+ * our own delivery instant), so a later delivery can legitimately carry an
77
+ * EARLIER number. Taking it would reopen a gap that is closed, and the next
78
+ * reconnect would post into the topic again.
79
+ */
80
+ export function advanceMark(prev, next) {
81
+ return { at: Math.max(prev?.at ?? 0, next.at), ...(next.text ? { text: next.text } : {}) };
82
+ }
83
+ /** The line that opens a replay, so nobody mistakes the gap for live traffic. */
84
+ export function replayHeader(posted, dropped) {
85
+ const what = `${posted} message${posted === 1 ? "" : "s"}`;
86
+ const older = dropped ? ` (+${dropped} older not shown)` : "";
87
+ return `— ${what} missed while this topic was disconnected${older} —`;
88
+ }
89
+ //# sourceMappingURL=tg-replay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tg-replay.js","sourceRoot":"","sources":["../src/tg-replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH;;;mEAGmE;AACnE,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAC;AAsC7B;;;;;;GAMG;AACH,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;AAEtD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,UAAU,CACxB,KAA4B,EAC5B,IAAsC,EACtC,GAAG,GAAW,UAAU;IAExB,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;IAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CACxB,CAAC,CAAC,EAAE,EAAE,CACJ,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,CAAC,EAAE,KAAK,QAAQ,IAAI,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAC5H,CAAC;IACF,6EAA6E;IAC7E,0EAA0E;IAC1E,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACtB,MAAM,IAAI,GACR,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC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version": "0.20.21",
4
4
  "main": "dist/session.js",
5
5
  "scripts": {
6
6
  "test": "node --import tsx --test --test-timeout=120000 test/*.ts context/*/test/*.test.mjs",
package/public/index.html CHANGED
@@ -4182,7 +4182,7 @@
4182
4182
  <div class="ib-url" id="inviteUrl"></div>
4183
4183
  </div>
4184
4184
  <hr class="peers-sep">
4185
- <p class="sec-note"><b>Invite an agent</b> — of yours, or a teammate's — to collaborate here. Like a user, but instead of a login it gets a <b>prompt to paste</b> into its own session. <b>restricted</b> (the default) only touches the agents it spawns; <b>member</b> acts like a user — it sees and drives the whole cockpit, never admin.</p>
4185
+ <p class="sec-note"><b>Invite an agent</b> — of yours, or a teammate's — to collaborate here. Like a user, but instead of a login it gets a <b>prompt to paste</b> into its own session. <b>restricted</b> (the default) only touches the agents it spawns; <b>member</b> acts like a user — it sees and drives the whole cockpit; <b>admin</b> also sees every group and may rewrite a profile's prompt. None of them can manage accounts, invite agents or change a profile's guardrails. You can change an agent's access below at any time — it keeps its key.</p>
4186
4186
  <ul id="peersList"></ul>
4187
4187
  <form id="peersAdd">
4188
4188
  <input id="peerName" placeholder="peer name (e.g. brasdroit1)" autocomplete="off" spellcheck="false">
@@ -4190,9 +4190,10 @@
4190
4190
  <!-- restricted FIRST, so the selected-by-default option is the narrow one:
4191
4191
  handing over the whole cockpit is a choice you make, never one you
4192
4192
  forget to unmake. -->
4193
- <select id="peerScope" title="restricted: only the agents it spawns. member: acts like a user (whole cockpit).">
4193
+ <select id="peerScope" title="restricted: only the agents it spawns. member: acts like a user (whole cockpit). admin: member, plus every group and profile prompts.">
4194
4194
  <option value="restricted">restricted (own agents)</option>
4195
4195
  <option value="member">member (whole cockpit)</option>
4196
+ <option value="admin">admin (+ profile prompts)</option>
4196
4197
  </select>
4197
4198
  <button type="submit">Invite agent</button>
4198
4199
  </form>
@@ -17304,13 +17305,36 @@
17304
17305
  const meta = document.createElement("div");
17305
17306
  const who = document.createElement("b"); who.textContent = p.name;
17306
17307
  const sub = document.createElement("div"); sub.className = "pmeta";
17307
- // Mirrors `peerScope` on the server: anything that is not an explicit
17308
- // `member` reads as restricted, so a row from before scopes is not
17309
- // labelled wider than it is.
17310
- sub.textContent = (p.scope === "member" ? "member" : "restricted")
17311
- + (p.note ? " · " + p.note : "")
17312
- + (p.pending ? " · not enrolled yet" : "");
17308
+ // Mirrors `peerScope` on the server: anything that is not an exact scope
17309
+ // name reads as restricted, so a row from before scopes is not labelled
17310
+ // wider than it is.
17311
+ const SCOPES = ["restricted", "member", "admin"];
17312
+ const current = SCOPES.includes(p.scope) ? p.scope : "restricted";
17313
+ sub.textContent = [p.note, p.pending ? "not enrolled yet" : ""].filter(Boolean).join(" · ");
17313
17314
  meta.append(who, sub);
17315
+ // The access is changed in place: the agent keeps its key, so nothing has
17316
+ // to be pasted again on its side.
17317
+ const sel = document.createElement("select");
17318
+ sel.title = "what this agent may touch here — change it at any time, it keeps its key";
17319
+ for (const s of SCOPES) {
17320
+ const o = document.createElement("option");
17321
+ o.value = s; o.textContent = s; o.selected = s === current;
17322
+ sel.appendChild(o);
17323
+ }
17324
+ sel.addEventListener("change", async () => {
17325
+ const next = sel.value;
17326
+ const wider = SCOPES.indexOf(next) > SCOPES.indexOf(current);
17327
+ const what = next === "admin"
17328
+ ? "It will see every group and may rewrite any profile's prompt."
17329
+ : next === "member" ? "It will see and drive every agent on this cockpit." : "It will only touch the agents it spawned.";
17330
+ if (wider && !confirm("Give " + p.name + " " + next + " access?\n\n" + what)) { sel.value = current; return; }
17331
+ const r = await fetch("/peers", {
17332
+ method: "PUT", headers: { "Content-Type": "application/json" },
17333
+ body: JSON.stringify({ name: p.name, scope: next }),
17334
+ });
17335
+ if (!r.ok) { const out = await r.json().catch(() => ({})); alert(out.error || "failed"); }
17336
+ renderPeers();
17337
+ });
17314
17338
  const del = document.createElement("button");
17315
17339
  del.textContent = "revoke"; del.style.color = "#e26";
17316
17340
  del.addEventListener("click", async () => {
@@ -17318,7 +17342,7 @@
17318
17342
  await fetch("/peers?name=" + encodeURIComponent(p.name), { method: "DELETE" });
17319
17343
  renderPeers();
17320
17344
  });
17321
- const btns = document.createElement("div"); btns.className = "u-actions"; btns.append(del);
17345
+ const btns = document.createElement("div"); btns.className = "u-actions"; btns.append(sel, del);
17322
17346
  li.append(meta, btns);
17323
17347
  ul.appendChild(li);
17324
17348
  }