samagotchi 0.192.0 → 0.193.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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -1
  3. data/docs/architecture.md +3 -3
  4. data/docs/bundles.md +45 -28
  5. data/docs/cli.md +22 -8
  6. data/docs/configuration.md +4 -3
  7. data/docs/context.md +8 -5
  8. data/docs/guardrails.md +9 -4
  9. data/docs/internals/bridge.md +3 -1
  10. data/docs/internals/bundles.md +2 -2
  11. data/docs/internals/pages.md +4 -4
  12. data/docs/internals/participants.md +5 -3
  13. data/docs/internals/peers.md +75 -22
  14. data/docs/internals/prompt-caching.md +2 -1
  15. data/docs/internals/remote-sessions.md +6 -0
  16. data/docs/internals/session-lifecycle.md +3 -2
  17. data/docs/internals/web-frontend.md +91 -19
  18. data/docs/internals/working-set.md +5 -1
  19. data/docs/peers.md +71 -22
  20. data/docs/plugins.md +7 -4
  21. data/docs/releasing.md +30 -2
  22. data/docs/scheduling.md +3 -2
  23. data/docs/sessions.md +1 -1
  24. data/docs/testing.md +35 -3
  25. data/docs/thin-clients.md +27 -11
  26. data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
  27. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
  28. data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
  29. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
  30. data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
  31. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
  32. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  33. data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
  34. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  35. data/lib/samagotchi/compat.rb +8 -1
  36. data/lib/samagotchi/config.rb +4 -0
  37. data/lib/samagotchi/continue_offer.rb +13 -1
  38. data/lib/samagotchi/delivery.rb +2 -2
  39. data/lib/samagotchi/engine/models.rb +17 -0
  40. data/lib/samagotchi/engine/plugins.rb +2 -0
  41. data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
  42. data/lib/samagotchi/jsonl_store.rb +13 -2
  43. data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
  44. data/lib/samagotchi/llm/openai_chat.rb +5 -0
  45. data/lib/samagotchi/llm/prompt_cache.rb +5 -0
  46. data/lib/samagotchi/pages/launch.rb +1 -1
  47. data/lib/samagotchi/peer_cold.rb +92 -0
  48. data/lib/samagotchi/peer_command.rb +24 -9
  49. data/lib/samagotchi/peer_continue.rb +32 -16
  50. data/lib/samagotchi/peer_exchanges.rb +7 -0
  51. data/lib/samagotchi/peer_mailbox.rb +21 -4
  52. data/lib/samagotchi/peer_receipts.rb +33 -6
  53. data/lib/samagotchi/peer_say.rb +34 -16
  54. data/lib/samagotchi/plugin/api.rb +2 -1
  55. data/lib/samagotchi/plugin/context.rb +13 -1
  56. data/lib/samagotchi/plugin/peer.rb +118 -53
  57. data/lib/samagotchi/plugin/peers.rb +13 -6
  58. data/lib/samagotchi/session_access/local.rb +98 -37
  59. data/lib/samagotchi/session_access/remote_peers.rb +3 -3
  60. data/lib/samagotchi/session_chain/links.rb +8 -3
  61. data/lib/samagotchi/session_continue.rb +7 -6
  62. data/lib/samagotchi/session_manager/spawn.rb +6 -1
  63. data/lib/samagotchi/session_stores.rb +16 -11
  64. data/lib/samagotchi/system_one/runs.rb +5 -2
  65. data/lib/samagotchi/testing/boundary.rb +0 -1
  66. data/lib/samagotchi/testing/host.rb +1 -0
  67. data/lib/samagotchi/testing/recorder.rb +3 -0
  68. data/lib/samagotchi/version.rb +1 -1
  69. data/lib/samagotchi/web/peer_routes.rb +10 -5
  70. data/lib/samagotchi/web/public/agent_rows.js +206 -0
  71. data/lib/samagotchi/web/public/agent_runs.js +595 -0
  72. data/lib/samagotchi/web/public/answer_fold.js +41 -44
  73. data/lib/samagotchi/web/public/app.js +3 -3
  74. data/lib/samagotchi/web/public/block_fix.js +1 -1
  75. data/lib/samagotchi/web/public/cards_place.js +6 -4
  76. data/lib/samagotchi/web/public/history_reveal.js +5 -2
  77. data/lib/samagotchi/web/public/index.html +38 -2
  78. data/lib/samagotchi/web/public/page_view.js +1 -1
  79. data/lib/samagotchi/web/public/pages.css +1 -7
  80. data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
  81. data/lib/samagotchi/web/public/peer_note.js +4 -2
  82. data/lib/samagotchi/web/public/peer_row.js +37 -0
  83. data/lib/samagotchi/web/public/quiet_notes.js +12 -1
  84. data/lib/samagotchi/web/public/revise_stack.js +5 -2
  85. data/lib/samagotchi/web/public/send_turn.js +11 -3
  86. data/lib/samagotchi/web/public/turn_block.js +4 -0
  87. data/lib/samagotchi/worker/mail.rb +11 -1
  88. data/lib/samagotchi/worker.rb +7 -5
  89. data/lib/samagotchi/worker_inbound/mail.rb +34 -6
  90. data/lib/samagotchi/worker_inbound.rb +11 -3
  91. data/lib/samagotchi/worker_link.rb +13 -1
  92. data/lib/samagotchi/worker_wakes.rb +33 -5
  93. metadata +5 -2
  94. data/lib/samagotchi/web/public/pages_group.js +0 -67
@@ -65,6 +65,7 @@ module Samagotchi
65
65
  cost: -> { cost(recorder[:cost]) },
66
66
  usage: -> { usage(recorder[:usage]) },
67
67
  turn_author: -> { recorder[:turn_author] },
68
+ turn_interactive: -> { recorder[:turn_interactive] == true },
68
69
  cache_confirm: lambda { |line, changed|
69
70
  CacheConfirm.ask(line: line, cost: :full, changed: changed,
70
71
  engine: CacheEngine.new(recorder[:history], recorder[:context_tokens]))
@@ -45,6 +45,9 @@ module Samagotchi
45
45
  usage: nil,
46
46
  # ctx.turn_author and the hooks' author: (a participant's "p:<id>"), nil for the owner.
47
47
  turn_author: nil,
48
+ # ctx.turn_interactive?: the running turn is the owner's from a web tab
49
+ # or a TUI (false: a schedule's, a delegate's, a wake turn, chi send's).
50
+ turn_interactive: true,
48
51
  # ctx.cache_confirm: whether the session has a model reply (a cache
49
52
  # to restart), and the context size its confirm names (nil: none).
50
53
  history: false, context_tokens: nil,
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.192.0"
4
+ VERSION = "0.193.0"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end
@@ -78,17 +78,20 @@ module Samagotchi
78
78
  end
79
79
 
80
80
  # DELETE /api/sessions/:id/peers/:name (Unpair) → {unpaired: whether
81
- # there was a pairing}; the messages stay.
81
+ # there was a pairing, forwarded_to: the chain's live link it ended too,
82
+ # when the session was continued}; the messages stay.
82
83
  def handle_peer_unpair(_req, id, name)
83
84
  @session_class.load(id, state_dir: default_state_dir)
84
85
  return invalid_peer(name) unless PeerMailbox.valid_name?(name)
85
86
 
86
- json_response(200, { unpaired: session_access.peer_unpair(id, name) })
87
+ ended = session_access.peer_unpair(id, name)
88
+ json_response(200, { unpaired: ended.unpaired, forwarded_to: ended.forwarded_to }.compact)
87
89
  end
88
90
 
89
91
  # POST /api/sessions/:id/peers/:name/pair {address, from} → 200
90
- # {status: "paired", link}; 409 {status: continued (next_id) |
91
- # old_worker, detail}.
92
+ # {status: "paired", link, forwarded_to: the chain's live link it
93
+ # paired, when the session was continued}; 409 {status: continued
94
+ # (next_id) | old_worker, detail}.
92
95
  def handle_peer_pair(req, id, name)
93
96
  body = body_hash(req, RequestBody::CONTEXT_PUSH_BODY_LIMIT) or return error_response(400, "invalid_json", "invalid JSON body")
94
97
  return invalid_peer(name) unless PeerMailbox.valid_name?(name)
@@ -99,7 +102,9 @@ module Samagotchi
99
102
  end
100
103
 
101
104
  paired = session_access.peer_pair(id, name, address: address, from: from)
102
- return json_response(200, { status: "paired", link: paired.link.to_h }) if paired.status == :paired
105
+ if paired.status == :paired
106
+ return json_response(200, { status: "paired", link: paired.link.to_h, forwarded_to: paired.forwarded_to }.compact)
107
+ end
103
108
 
104
109
  json_response(409, { status: paired.status.to_s, next_id: paired.next_id, in_flight: (true if paired.in_flight),
105
110
  detail: paired.refusal }.compact)
@@ -0,0 +1,206 @@
1
+ // The closed row of an item agents write to each other: a delegate report
2
+ // (a user line chi brought in, data-source "delegate_report") and a peer's
3
+ // answer (peer_note.js, `.bubble.note.peer-answer`). Its element and its
4
+ // words, drawn from the bubble it sits before: a report's "delegate report ·
5
+ // ‹first line› · N lines", a peer answer's "<name> <verdict> <summary> N
6
+ // lines" (peer_row.js). Which bubble gets one, its key and its stored open
7
+ // state are the owner's (answer_fold.js); this module only draws.
8
+ //
9
+ // A module of its own: static JS is cached at unversioned URLs, and a new
10
+ // export on an old module would blank the page. It imports only what
11
+ // answer_fold.js exported in the last release, and neither module uses the
12
+ // other's bindings at its top level (answer_fold.js re-exports reportLabel
13
+ // from here: an import cycle that is safe that way).
14
+ import { bubbleCopyText } from "./copy.js";
15
+ import { clip } from "./format.js";
16
+ import { foldLabel, foldRowText, lineCount } from "./answer_fold.js";
17
+ import { peerRowWords } from "./peer_row.js";
18
+ import { rowLabel } from "./quiet_notes.js";
19
+
20
+ export const REPORTS = '.bubble.user[data-source="delegate_report"]';
21
+ export const PEERS = ".bubble.note.peer-answer";
22
+ export const AGENT_ITEMS = `${REPORTS}, ${PEERS}`;
23
+
24
+ const ROW_CLASS = "answer-fold";
25
+ // A row's label, at most this long (CSS cuts it to the row's width).
26
+ const LABEL_CHARS = 160;
27
+
28
+ export const isReport = (bubble) => !!bubble?.matches?.(REPORTS);
29
+ export const isPeer = (bubble) => !!bubble?.matches?.(PEERS);
30
+
31
+ // A delegate report's lines before its body: "session: <id>",
32
+ // "status: <s>", "---".
33
+ const REPORT_HEADER = /^(?:(?:session|status):\s|---\s*$)/;
34
+
35
+ // A report row's label: its origin label (the bubble's) and its first
36
+ // heading or first line after the header.
37
+ export function reportLabel(text, origin = "") {
38
+ const body = String(text ?? "").replace(/\r\n?/g, "\n").split("\n").filter((l) => !REPORT_HEADER.test(l.trim())).join("\n");
39
+ const first = foldLabel(body);
40
+ return clip([origin.trim(), first].filter(Boolean).join(" · "), LABEL_CHARS);
41
+ }
42
+
43
+ // A peer answer's row words (peer_row.js) from its bubble: the peer's label
44
+ // (".peer-who" is "<label> answered"), the server's verdict (the pill's
45
+ // class), the summary it sent (none: `.note-text` is a fallback,
46
+ // peer_note.js marks it), the lines of its own text (`.peer-text`'s
47
+ // source; none without a body).
48
+ const VERDICTS = { ok: "OK", changes: "CHANGES", unsure: "UNSURE" };
49
+
50
+ // The server's verdict of a peer answer's bubble (OK, CHANGES, UNSURE), or
51
+ // null.
52
+ export function peerVerdict(bubble) {
53
+ const pill = bubble.querySelector(":scope > .peer-head .peer-verdict");
54
+ const verdict = pill ? Object.keys(VERDICTS).find((k) => pill.classList.contains(k)) : null;
55
+ return verdict ? VERDICTS[verdict] : null;
56
+ }
57
+
58
+ // A peer answer's name as its bubble says it ("<label> answered").
59
+ export function peerName(bubble) {
60
+ const who = bubble.querySelector(":scope > .peer-head > .peer-who")?.textContent || "";
61
+ return who.replace(/\s+answered\s*$/, "").trim() || bubble.dataset.peerName || "peer";
62
+ }
63
+
64
+ function peerRowOf(bubble) {
65
+ const note = bubble.querySelector(":scope > .note-text");
66
+ const body = bubble.querySelector(":scope > .peer-text");
67
+ const text = body ? (body.dataset.peerMd ?? body.textContent) : null;
68
+ const who = bubble.querySelector(":scope > .peer-head > .peer-who")?.textContent || "";
69
+ return peerRowWords({
70
+ name: who.replace(/\s+answered\s*$/, ""),
71
+ verdict: peerVerdict(bubble),
72
+ summary: note && !note.hasAttribute("data-fallback") ? note.textContent : "",
73
+ first: (text !== null ? foldLabel(text, { display: true }) : "") || note?.textContent || "",
74
+ lines: text !== null ? lineCount(text) : null,
75
+ });
76
+ }
77
+
78
+ // Writes only what changed: the owner's settle redraws every row, and a
79
+ // write that changes nothing still wakes the other observers on #history.
80
+ function setText(el, text) {
81
+ if (el && el.textContent !== text) el.textContent = text;
82
+ }
83
+
84
+ function setState(row, { folded, title, classes = {} }) {
85
+ const expanded = folded ? "false" : "true";
86
+ if (row.getAttribute("aria-expanded") !== expanded) row.setAttribute("aria-expanded", expanded);
87
+ if (row.title !== title) row.title = title;
88
+ for (const [name, on] of Object.entries({ folded, ...classes })) {
89
+ if (row.classList.contains(name) !== on) row.classList.toggle(name, on);
90
+ }
91
+ }
92
+
93
+ function drawPeerRow(row, bubble) {
94
+ const folded = bubble.classList.contains("folded");
95
+ const words = peerRowOf(bubble);
96
+ setState(row, { folded, title: folded ? "Show the answer" : "Fold the answer" });
97
+ for (const part of ["name", "verdict", "summary", "size"]) setText(row.querySelector(`.answer-fold-${part}`), words[part]);
98
+ }
99
+
100
+ // The row's spans: a report's own row "label size", a compact row (a peer
101
+ // answer's, a report's or a note's inside a run line, the mock-up's panel
102
+ // row) "name verdict summary size". Rebuilt when the shape changes.
103
+ const SHAPES = { label: ["label", "size"], compact: ["name", "verdict", "summary", "size"] };
104
+
105
+ function shape(row, want) {
106
+ if (row.dataset.shape === want) return;
107
+ const doc = row.ownerDocument;
108
+ row.replaceChildren(...SHAPES[want].map((part) => {
109
+ const el = doc.createElement("span");
110
+ el.className = `answer-fold-${part}`;
111
+ return el;
112
+ }));
113
+ row.dataset.shape = want;
114
+ }
115
+
116
+ const squash = (text) => String(text ?? "").replace(/\s+/g, " ").trim();
117
+
118
+ // A report's name in a line: the child's session ("child ab12cd", its
119
+ // header's `session:`), else its origin label.
120
+ function reportName(text, origin) {
121
+ const id = /^session:\s*(\S+)/m.exec(String(text ?? ""))?.[1];
122
+ return id ? `child ${id.slice(0, 6)}` : squash(origin) || "report";
123
+ }
124
+
125
+ // A report inside a run line: the mock-up's panel row, "child ab12cd ‹first
126
+ // line› N lines", opening to its text (CSS joins them).
127
+ function drawReportLineRow(row, bubble) {
128
+ shape(row, "compact");
129
+ const folded = bubble.classList.contains("folded");
130
+ const text = bubbleCopyText(bubble);
131
+ setState(row, {
132
+ folded, title: folded ? "Show the report" : "Fold the report",
133
+ classes: { report: false, prompt: false, steered: false, compact: true, "report-row": true },
134
+ });
135
+ const n = lineCount(text);
136
+ const words = { name: reportName(text, bubble.querySelector(":scope > .origin-label")?.textContent || ""), verdict: "",
137
+ summary: reportLabel(text), size: `${n} line${n === 1 ? "" : "s"}` };
138
+ for (const part of SHAPES.compact) setText(row.querySelector(`:scope > .answer-fold-${part}`), words[part]);
139
+ }
140
+
141
+ function drawReportRow(row, bubble) {
142
+ shape(row, "label");
143
+ const folded = bubble.classList.contains("folded");
144
+ const text = bubbleCopyText(bubble);
145
+ const own = { label: reportLabel(text, bubble.querySelector(":scope > .origin-label")?.textContent || "") };
146
+ const { label, size } = foldRowText(text, { folded, ...own });
147
+ setState(row, {
148
+ folded, title: folded ? "Show the report" : "Fold the report",
149
+ classes: { report: true, prompt: false, steered: bubble.classList.contains("steered"), compact: false, "report-row": false },
150
+ });
151
+ const [labelEl, sizeEl] = row.children;
152
+ setText(labelEl, label);
153
+ setText(sizeEl, size);
154
+ }
155
+
156
+ // Draws +row+ from +bubble+ (a report or a peer answer): its words, and its
157
+ // folded state from the bubble's class. +inLine+: under a run line, where a
158
+ // report's row is the compact panel row (a peer answer's always is).
159
+ export function drawAgentRow(row, bubble, { inLine = false } = {}) {
160
+ if (isPeer(bubble)) drawPeerRow(row, bubble);
161
+ else if (inLine) drawReportLineRow(row, bubble);
162
+ else drawReportRow(row, bubble);
163
+ }
164
+
165
+ // A quiet item's words as its compact row in a line: its name (the notes
166
+ // row's, quiet_notes.js) and its text after that name.
167
+ export function noteRowWords(el) {
168
+ const name = rowLabel([el]).names[0] || "note";
169
+ let text;
170
+ if (el.matches(".steer-row")) text = el.querySelector(".steer-text")?.textContent;
171
+ else if (el.querySelector(":scope > .note-text")) text = el.querySelector(":scope > .note-text").textContent;
172
+ else text = el.textContent;
173
+ let said = squash(text);
174
+ if (said.startsWith(`${name}: `)) said = said.slice(name.length + 2);
175
+ if (el.matches(".objectives-entry")) said = said.replace(/^Objectives\s*/, "");
176
+ return { name, summary: said };
177
+ }
178
+
179
+ // A note's compact row in a line (closed: the note is hidden under it; a tap
180
+ // shows it, never stored).
181
+ export function makeNoteRow(doc) {
182
+ const row = doc.createElement("button");
183
+ row.type = "button";
184
+ row.className = `${ROW_CLASS} compact note-row folded`;
185
+ shape(row, "compact");
186
+ row.setAttribute("aria-expanded", "false");
187
+ return row;
188
+ }
189
+
190
+ export function drawNoteRow(row, el, open) {
191
+ const { name, summary } = noteRowWords(el);
192
+ setState(row, { folded: !open, title: open ? "Hide the note" : "Show the note" });
193
+ for (const [part, text] of [["name", name], ["verdict", ""], ["summary", summary], ["size", ""]]) {
194
+ setText(row.querySelector(`:scope > .answer-fold-${part}`), text);
195
+ }
196
+ }
197
+
198
+ // A new row for +bubble+ (a report or a peer answer), not drawn yet.
199
+ export function makeAgentRow(doc, bubble) {
200
+ const peer = isPeer(bubble);
201
+ const row = doc.createElement("button");
202
+ row.type = "button";
203
+ row.className = peer ? `${ROW_CLASS} peer compact` : ROW_CLASS;
204
+ shape(row, peer ? "compact" : "label");
205
+ return row;
206
+ }