samagotchi 0.187.0 → 0.188.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 (137) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -1
  3. data/bin/chi +2 -0
  4. data/docs/architecture.md +8 -4
  5. data/docs/bundles.md +124 -120
  6. data/docs/cli.md +25 -5
  7. data/docs/configuration.md +4 -4
  8. data/docs/context.md +18 -162
  9. data/docs/guardrails.md +3 -1
  10. data/docs/hooks.md +2 -1
  11. data/docs/internals/attached-context.md +19 -37
  12. data/docs/internals/bridge.md +1 -0
  13. data/docs/internals/engine.md +2 -2
  14. data/docs/internals/llm-context-forget.md +5 -6
  15. data/docs/internals/objectives.md +1 -0
  16. data/docs/internals/pages.md +6 -3
  17. data/docs/internals/peers.md +149 -0
  18. data/docs/internals/remote-sessions.md +7 -4
  19. data/docs/internals/web-frontend.md +61 -37
  20. data/docs/internals/web-push.md +1 -1
  21. data/docs/memory.md +1 -1
  22. data/docs/pages.md +2 -1
  23. data/docs/peers.md +141 -0
  24. data/docs/plugins.md +52 -27
  25. data/docs/sessions.md +8 -4
  26. data/docs/sub-agent.md +5 -5
  27. data/lib/samagotchi/bridge.rb +3 -3
  28. data/lib/samagotchi/bundle_command/api_calls.rb +2 -1
  29. data/lib/samagotchi/bundle_command/check.rb +1 -1
  30. data/lib/samagotchi/bundles/claude-peer/manifest.yml +11 -18
  31. data/lib/samagotchi/bundles/claude-peer/plugin.rb +22 -16
  32. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +48 -39
  33. data/lib/samagotchi/bundles/claude-peer/scripts/pair +15 -73
  34. data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +16 -16
  35. data/lib/samagotchi/bundles/codex-peer/manifest.yml +11 -15
  36. data/lib/samagotchi/bundles/codex-peer/plugin.rb +35 -24
  37. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +51 -40
  38. data/lib/samagotchi/bundles/codex-peer/scripts/pair +18 -54
  39. data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +16 -16
  40. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  41. data/lib/samagotchi/bundles/skills/manifest.yml +2 -2
  42. data/lib/samagotchi/bundles/skills/plugin.rb +14 -8
  43. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  44. data/lib/samagotchi/client_id.rb +15 -1
  45. data/lib/samagotchi/config.rb +8 -7
  46. data/lib/samagotchi/context_absorber.rb +23 -277
  47. data/lib/samagotchi/context_sources.rb +17 -69
  48. data/lib/samagotchi/engine/messages.rb +12 -1
  49. data/lib/samagotchi/engine/plugins.rb +1 -1
  50. data/lib/samagotchi/engine.rb +7 -6
  51. data/lib/samagotchi/engine_peers.rb +0 -3
  52. data/lib/samagotchi/guardrail_wiring.rb +2 -0
  53. data/lib/samagotchi/guardrails/chi_dirs.rb +4 -3
  54. data/lib/samagotchi/guardrails/parent_approvals.rb +6 -5
  55. data/lib/samagotchi/guardrails/protected_paths.rb +6 -3
  56. data/lib/samagotchi/inline_widgets.rb +11 -3
  57. data/lib/samagotchi/log_line.rb +1 -1
  58. data/lib/samagotchi/note_move.rb +3 -1
  59. data/lib/samagotchi/page_command.rb +18 -9
  60. data/lib/samagotchi/pages/builder.rb +7 -8
  61. data/lib/samagotchi/pages/launch.rb +8 -2
  62. data/lib/samagotchi/pages/store.rb +11 -2
  63. data/lib/samagotchi/peer_command.rb +192 -0
  64. data/lib/samagotchi/peer_continue.rb +99 -0
  65. data/lib/samagotchi/peer_exchanges.rb +3 -3
  66. data/lib/samagotchi/peer_mailbox.rb +360 -0
  67. data/lib/samagotchi/peer_note.rb +208 -0
  68. data/lib/samagotchi/peer_receipts.rb +247 -0
  69. data/lib/samagotchi/peer_registry.rb +44 -0
  70. data/lib/samagotchi/peer_roster.rb +79 -0
  71. data/lib/samagotchi/peer_say.rb +168 -0
  72. data/lib/samagotchi/plugin/api.rb +29 -21
  73. data/lib/samagotchi/plugin/context.rb +7 -0
  74. data/lib/samagotchi/plugin/loader.rb +3 -3
  75. data/lib/samagotchi/plugin/peer.rb +175 -64
  76. data/lib/samagotchi/plugin/peers.rb +70 -0
  77. data/lib/samagotchi/run_options.rb +2 -1
  78. data/lib/samagotchi/schedule_command.rb +10 -3
  79. data/lib/samagotchi/schedule_display.rb +32 -0
  80. data/lib/samagotchi/session_access/contract.rb +18 -1
  81. data/lib/samagotchi/session_access/local.rb +132 -14
  82. data/lib/samagotchi/session_access/remote.rb +4 -2
  83. data/lib/samagotchi/session_access/remote_peers.rb +75 -0
  84. data/lib/samagotchi/session_continue.rb +12 -0
  85. data/lib/samagotchi/session_inbox_delivery.rb +5 -2
  86. data/lib/samagotchi/session_manager/delivery.rb +5 -3
  87. data/lib/samagotchi/session_manager/removal.rb +2 -0
  88. data/lib/samagotchi/session_manager/spawn.rb +2 -1
  89. data/lib/samagotchi/session_manager.rb +1 -0
  90. data/lib/samagotchi/session_stores.rb +17 -0
  91. data/lib/samagotchi/steer.rb +1 -1
  92. data/lib/samagotchi/system_prompt.rb +1 -1
  93. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  94. data/lib/samagotchi/terminal_ui/formatting.rb +4 -1
  95. data/lib/samagotchi/testing/boundary.rb +4 -13
  96. data/lib/samagotchi/testing/bundle.rb +40 -5
  97. data/lib/samagotchi/testing/install.rb +4 -4
  98. data/lib/samagotchi/testing/recording_api.rb +2 -1
  99. data/lib/samagotchi/tools/context_read.rb +0 -14
  100. data/lib/samagotchi/tools/list_schedules.rb +8 -3
  101. data/lib/samagotchi/version.rb +1 -1
  102. data/lib/samagotchi/web/app.rb +2 -13
  103. data/lib/samagotchi/web/message_display.rb +4 -2
  104. data/lib/samagotchi/web/peer_routes.rb +133 -0
  105. data/lib/samagotchi/web/public/app.js +11 -11
  106. data/lib/samagotchi/web/public/bubbles.js +2 -2
  107. data/lib/samagotchi/web/public/cards_place.js +20 -1
  108. data/lib/samagotchi/web/public/context_chips.js +45 -46
  109. data/lib/samagotchi/web/public/history_reveal.js +4 -1
  110. data/lib/samagotchi/web/public/history_shape.js +3 -2
  111. data/lib/samagotchi/web/public/index.html +13 -0
  112. data/lib/samagotchi/web/public/notify.js +1 -1
  113. data/lib/samagotchi/web/public/page_view.js +2 -1
  114. data/lib/samagotchi/web/public/peer_chip.js +60 -74
  115. data/lib/samagotchi/web/public/peer_data.js +13 -11
  116. data/lib/samagotchi/web/public/peer_links.js +12 -7
  117. data/lib/samagotchi/web/public/peer_mailbox_api.js +20 -0
  118. data/lib/samagotchi/web/public/peer_note.js +7 -3
  119. data/lib/samagotchi/web/public/peer_text.js +4 -4
  120. data/lib/samagotchi/web/public/quiet_notes.js +199 -0
  121. data/lib/samagotchi/web/public/revise_stack.js +4 -3
  122. data/lib/samagotchi/web/public/session_view.js +2 -2
  123. data/lib/samagotchi/web/public/stream_handlers.js +3 -2
  124. data/lib/samagotchi/web/public/turn_block.js +7 -3
  125. data/lib/samagotchi/web/public/turn_events.js +6 -0
  126. data/lib/samagotchi/web/push/attention.rb +3 -2
  127. data/lib/samagotchi/web/session_access_routes.rb +3 -20
  128. data/lib/samagotchi/worker/leave.rb +3 -0
  129. data/lib/samagotchi/worker/mail.rb +73 -0
  130. data/lib/samagotchi/worker/startup.rb +3 -0
  131. data/lib/samagotchi/worker/wake_turns.rb +20 -5
  132. data/lib/samagotchi/worker.rb +12 -6
  133. data/lib/samagotchi/worker_inbound/mail.rb +214 -0
  134. data/lib/samagotchi/worker_inbound.rb +43 -46
  135. data/lib/samagotchi/worker_wakes.rb +26 -21
  136. metadata +19 -2
  137. data/lib/samagotchi/peer_sources.rb +0 -58
@@ -88,6 +88,18 @@ main{flex:1;display:flex;flex-direction:column;gap:16px;padding:max(16px,var(--s
88
88
  #history>.bubble.folded{display:none}
89
89
  .answer-fold{display:flex;align-items:baseline;gap:0;align-self:stretch;max-width:100%;min-width:0;margin:0;padding:12px 14px;font:inherit;font-size:13px;font-weight:600;line-height:1.5;color:var(--text-muted);text-align:left;cursor:pointer;user-select:none;background:var(--bg-panel);border:1px solid var(--border);border-left:3px solid var(--accent-strong);border-radius:12px}
90
90
  @media(hover:hover){.answer-fold:hover .answer-fold-size{color:var(--text-2)}}
91
+ /* The notes row (quiet_notes.js): a stretch of background items folded, a sibling before them. */
92
+ #history>.quiet-hidden{display:none}
93
+ .quiet-notes{display:flex;align-items:baseline;gap:6px;align-self:stretch;max-width:100%;min-width:0;min-height:32px;margin:0;padding:5px 12px;font:inherit;font-size:13px;line-height:1.5;color:var(--text-muted);text-align:left;cursor:pointer;user-select:none;background:transparent;border:0;border-left:3px solid var(--text-faint);border-radius:0 8px 8px 0}
94
+ @media(hover:hover){.quiet-notes:hover{background:var(--bg-hover);color:var(--text-2)}}
95
+ .quiet-notes:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
96
+ .quiet-notes::before{content:"▸";color:var(--text-faint);flex:none}
97
+ .quiet-notes.open::before{content:"▾"}
98
+ .quiet-notes.warn{border-left-color:var(--warn)}
99
+ .qn-count{flex:none;font-weight:600;white-space:nowrap}
100
+ .qn-names{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
101
+ .qn-warn{flex:none;margin-left:auto;white-space:nowrap;color:var(--warn-text)}
102
+ @media(max-width:600px){.quiet-notes{min-height:44px;align-items:center}}
91
103
  .answer-fold:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
92
104
  .answer-fold::before{content:"▾\00a0";color:var(--accent);flex:none}
93
105
  .answer-fold.folded::before{content:"▸\00a0"}
@@ -1478,6 +1490,7 @@ body.has-session.composer-folded #infoBar{border-top-color:transparent}
1478
1490
  .peer-pop-legs{display:flex;flex-direction:column;gap:6px}
1479
1491
  .peer-pop-legs>div{display:flex;gap:8px}
1480
1492
  .peer-pop-legs>div>span:first-child{flex:0 0 78px;color:var(--text-muted)}
1493
+ .peer-pop-legs>div>span:last-child{min-width:0;overflow-wrap:anywhere;word-break:break-word}
1481
1494
  .peer-pop-legs .warn{color:var(--warn-text)}
1482
1495
  .peer-pop-list{display:flex;flex-direction:column}
1483
1496
  .peer-pop-list-head{font-size:12px;font-weight:700;color:var(--text-muted);padding-bottom:4px}
@@ -20,7 +20,7 @@ export function notifyState({ hasNotification, secureContext, permission, wanted
20
20
  // an attached context change (context) or a finished background task
21
21
  // (task): nobody started it, so nobody watched it.
22
22
  const DONE_MIN_SECONDS = 10;
23
- const UNWATCHED_ORIGINS = ["delegate_report", "context", "task"];
23
+ const UNWATCHED_ORIGINS = ["delegate_report", "context", "peer", "task"];
24
24
  const VERDICT_REASON_MAX = 120;
25
25
 
26
26
  // The step-limit question (kind "continue": a turn ran out of iterations
@@ -23,6 +23,7 @@ import { tableTemplate } from "./page_table.js";
23
23
  import { nextSort } from "./page_table_model.js";
24
24
  import { changedText, errorCopy, failureParts, keptAfterFailure, metaText } from "./page_model.js";
25
25
 
26
+ // Mirrors Pages::NAME (lib/samagotchi/pages.rb).
26
27
  const PAGE_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
27
28
 
28
29
  export function createPageView({
@@ -55,7 +56,7 @@ export function createPageView({
55
56
  async function open(name) {
56
57
  if (view?.name === name) return;
57
58
  const run = runs.get(name);
58
- const valid = PAGE_NAME.exec(name)?.[0] === name;
59
+ const valid = PAGE_NAME.test(name);
59
60
  view = { name, status: valid ? "loading" : "missing", page: null, error: null, running: !!run, stderr: "", menu: "closed", deleting: false,
60
61
  sorts: {}, opened: {} };
61
62
  draw();
@@ -1,11 +1,12 @@
1
- // A paired peer in the session bar: its two context sources (<name>-inbox,
2
- // <name>-outbox) drawn as one chip that says how the pairing stands
3
- // (paired, waiting, answered, can't send, answers only), and that chip's
4
- // popover (how chi and the peer reach each other, the last exchanges, View
1
+ // A paired peer in the session bar: one chip per peer the peers model
2
+ // (peer_data.js, GET …/peers: the session's mailbox) names as paired, that
3
+ // says how the pairing stands (paired, waiting, answered, can't send,
4
+ // answers only), and that chip's popover (how chi and the peer reach each
5
+ // other, the last exchanges with what chi did with each message, View
5
6
  // latest answer, Status card, Unpair). Pure templates and models: the
6
- // chips' controller (context_chips.js) draws them, the exchanges are the
7
- // peers model's (peer_data.js, GET …/peers). docs/internals/web-frontend.md,
8
- // The peer exchange. Its own module: new exports go in new modules.
7
+ // chips' controller (context_chips.js) draws them. docs/internals/
8
+ // web-frontend.md, The peer exchange. Its own module: new exports go in new
9
+ // modules.
9
10
 
10
11
  import { html, nothing } from "./vendor/lit-html.js";
11
12
  import { spanText, verdictWord } from "./peer_exchange.js";
@@ -18,46 +19,29 @@ export const peerKey = (name) => `peer:${name}`;
18
19
  // How many exchanges the popover lists (the newest).
19
20
  export const POPOVER_EXCHANGES = 8;
20
21
 
21
- // GET …/context's rows split: the peer sources (a row with `peer` and its
22
- // `leg`) grouped per peer, {key, name, label, inbox, outbox} (a leg's row
23
- // or null; a muted leg stays in its group), in the order their first leg
24
- // comes; and the rest, the ordinary rows, as they were.
25
- export function peerGroups(rows) {
26
- const peers = new Map();
27
- const rest = [];
28
- for (const row of rows || []) {
29
- if (!row) continue;
30
- if (!row.peer || !["inbox", "outbox"].includes(row.leg)) {
31
- rest.push(row);
32
- continue;
33
- }
34
- let group = peers.get(row.peer);
35
- if (!group) {
36
- group = { key: peerKey(row.peer), name: row.peer, label: row.label || row.peer, inbox: null, outbox: null };
37
- peers.set(row.peer, group);
38
- }
39
- group[row.leg] = row;
40
- }
41
- return { peers: [...peers.values()], rest };
22
+ // The peers model's rows as chips' groups: {key, name, label, peer} for each
23
+ // paired one (its link.json: paired_at), in the model's order. A peer that
24
+ // was unpaired keeps its messages but has no chip.
25
+ export function peerGroups(peers) {
26
+ return (peers || []).filter((peer) => peer?.name && peer.paired_at)
27
+ .map((peer) => ({ key: peerKey(peer.name), name: peer.name, label: peer.label || peer.name, peer }));
42
28
  }
43
29
 
44
- // A leg that counts: there and not muted.
45
- const live = (leg) => (leg && !leg.muted ? leg : null);
46
- // The group's legs as the chip reads them, and whether it shows at all
47
- // (a group whose legs are all muted is gone from the bar, as a muted row is).
48
- export const peerShown = (group) => !!(live(group.inbox) || live(group.outbox));
30
+ // A message no turn has read yet: no receipt (chi hasn't taken it), or one
31
+ // that holds it as a note (held, noted).
32
+ const unread = (message) => !message?.receipt || ["held", "noted"].includes(message.receipt.state);
49
33
 
50
34
  // The sends among +exchanges+ (newest first): an unclaimed answer has no id.
51
35
  const sends = (exchanges) => (exchanges || []).filter((ex) => ex?.id);
52
36
  const ASKS = ["validate", "question"];
37
+ const messagesOf = (group) => group?.peer?.messages || [];
53
38
 
54
39
  // The newest send failed and the peer hasn't paired again since: pairing
55
- // again (the pair script) pushes the inbox, which moves its row's
56
- // fetched_at past the failed send's time.
40
+ // again writes a new link (paired_at) after the failed send's time.
57
41
  export function cantSend(group, exchanges) {
58
42
  const newest = sends(exchanges)[0];
59
43
  if (newest?.state !== "failed") return false;
60
- const paired = Date.parse(live(group.inbox)?.fetched_at || "");
44
+ const paired = Date.parse(group?.peer?.paired_at || "");
61
45
  const failed = Date.parse(newest.at || "");
62
46
  return !(paired > failed);
63
47
  }
@@ -67,26 +51,24 @@ const since = (iso, now) => {
67
51
  };
68
52
 
69
53
  // The chip of +group+: the first state that applies.
70
- // - answers-only: no inbox leg (chi can't send);
54
+ // - answers-only: the pairing has no address (chi can't send);
71
55
  // - cant-send: the newest send failed (the transport refused it);
72
56
  // - waiting: the newest validate or question send has no answer (its age the send's);
73
- // - answered: the outbox has a text the agent hasn't read (its age the text's), with the dot;
57
+ // - answered: its newest message no turn has read (its age the message's), with the dot;
74
58
  // - paired otherwise.
75
- // +exchanges+: the peer's from GET …/peers, newest first ([] when not read,
76
- // an older chi web, a peer the read doesn't name: nothing then says failed).
59
+ // +exchanges+: the peer's from GET …/peers, newest first.
77
60
  // @return {key, name, state, label, title, dot}
78
61
  export function peerChipModel(group, exchanges = [], now = Date.now()) {
79
- const inbox = live(group.inbox);
80
- const outbox = live(group.outbox);
81
62
  const all = sends(exchanges);
82
63
  const ask = all.find((ex) => ASKS.includes(ex.kind));
64
+ const newest = messagesOf(group)[0];
83
65
  let state = "paired";
84
66
  let words = "paired";
85
- let why = "paired: chi can send to it and it answers into its outbox";
86
- if (!inbox) {
67
+ let why = "paired: chi can send to it and it answers with chi peer say";
68
+ if (!group.peer?.can_send) {
87
69
  state = "answers-only";
88
70
  words = "answers only";
89
- why = "no inbox attached here: it answers, chi can't send to it";
71
+ why = "paired with no address chi can reach: it answers, chi can't send to it";
90
72
  } else if (cantSend(group, exchanges)) {
91
73
  state = "cant-send";
92
74
  words = "can't send";
@@ -97,11 +79,11 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
97
79
  words = age === null ? "waiting" : `waiting ${spanText(age)}`;
98
80
  why = [ask.kind, ask.title].filter(Boolean).join(" · ") || "a send";
99
81
  why = `waiting for its answer to ${why}`;
100
- } else if (outbox?.unread && outbox.has_text) {
101
- const age = since(outbox.fetched_at, now);
82
+ } else if (newest && unread(newest)) {
83
+ const age = since(newest.at, now);
102
84
  state = "answered";
103
85
  words = age === null ? "answered" : `answered ${spanText(age)}`;
104
- why = "an answer the agent hasn't read yet";
86
+ why = "a message the agent hasn't read yet";
105
87
  }
106
88
  return { key: group.key, name: group.key, state, label: `${group.label} · ${words}`, title: why,
107
89
  dot: state === "answered", peer: true };
@@ -109,58 +91,62 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
109
91
 
110
92
  // An exchange's state as the popover's list says it: "waiting 2m",
111
93
  // "changes · 11m", "ok · 4m", "answered · 4m", "no answer expected",
112
- // "not sent"; +kind+ its tint.
94
+ // "not sent", then what chi did with the answer's message (+words+, the
95
+ // server's: "held · peer turns", "read", "completed"); +kind+ its tint.
113
96
  // @return {text, kind}
114
- export function exchangeState(ex, now = Date.now()) {
97
+ export function exchangeState(ex, now = Date.now(), words = null) {
115
98
  if (ex.state === "failed") return { text: "not sent", kind: "failed" };
116
99
  if (ex.state === "none_expected") return { text: "no answer expected", kind: "none" };
117
100
  if (ex.state === "waiting") {
118
101
  const age = since(ex.at, now);
119
102
  return { text: age === null ? "waiting" : `waiting ${spanText(age)}`, kind: "waiting" };
120
103
  }
121
- const took = ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
104
+ const took = ex.id && ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
122
105
  const verdict = verdictWord(ex.answer?.verdict);
123
106
  const word = verdict || "answered";
124
- return { text: took === null ? word : `${word} · ${spanText(took)}`, kind: verdict || "answered" };
107
+ const text = [took === null ? word : `${word} · ${spanText(took)}`, words].filter(Boolean).join(" · ");
108
+ return { text, kind: verdict || "answered" };
125
109
  }
126
110
 
127
111
  // The popover's inside for +group+ (scene 2). +exchanges+ newest first;
128
- // +text+ the outbox's text once View latest answer read it (null before);
129
- // +status+ the status card's command ("/claude") when the session has it,
130
- // else null; +drawn+(id) whether that send's row is in the conversation
131
- // (its list row then jumps to it).
112
+ // +text+ the newest message's text once View latest answer read it (null
113
+ // before); +status+ the status card's command ("/claude") when the session
114
+ // has it, else null; +drawn+(id) whether that send's row is in the
115
+ // conversation (its list row then jumps to it).
132
116
  export function peerPopoverTemplate(group, exchanges = [], { text = null, status = null, drawn = () => false, now = Date.now() } = {}) {
133
- const inbox = live(group.inbox);
134
- const outbox = live(group.outbox);
117
+ const peer = group.peer || {};
118
+ const messages = messagesOf(group);
119
+ const words = new Map(messages.map((m) => [String(m.serial), m.words]));
135
120
  const all = sends(exchanges);
136
121
  const lastSent = all.find((ex) => ex.state !== "failed");
137
- const sendsLine = !inbox
138
- ? "can't: no inbox attached here"
139
- : cantSend(group, exchanges)
122
+ const failing = peer.can_send && cantSend(group, exchanges);
123
+ const sendsLine = !peer.can_send
124
+ ? "can't: it paired with no address chi can reach"
125
+ : failing
140
126
  ? `the last send didn't go through: pair again from ${group.label}`
141
- : [`to its inbox (${group.inbox.name})`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
142
- const answersLine = outbox
143
- ? [`into ${outbox.name}`, outbox.fetched_at ? `last ${wallClock(outbox.fetched_at, now)}` : "nothing yet"].join(" · ")
144
- : "no outbox attached";
145
- const list = all.slice(0, POPOVER_EXCHANGES);
127
+ : [`to ${peer.address}`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
128
+ const answersLine = messages.length ? `with chi peer say · last ${wallClock(messages[0].at, now)}` : "nothing yet";
129
+ const list = (exchanges || []).filter((ex) => ex?.id || ex?.answer).slice(0, POPOVER_EXCHANGES);
146
130
  const rowInside = (ex) => {
147
- const state = exchangeState(ex, now);
148
- return html`<span class="pp-x-at">${wallClock(ex.at, now)}</span><span class="pp-x-what">${
149
- [ex.kind, ex.title].filter(Boolean).join(" · ") || "a send"}</span><span class=${`pp-x-state ${state.kind}`}>${state.text}</span>`;
131
+ const serial = ex.answer?.serial;
132
+ const state = exchangeState(ex, now, serial === undefined || serial === null ? null : words.get(String(serial)));
133
+ const what = ex.id ? [ex.kind, ex.title].filter(Boolean).join(" · ") || "a send"
134
+ : [`#${serial ?? "?"}`, ex.answer?.summary].filter(Boolean).join(" · ");
135
+ return html`<span class="pp-x-at">${wallClock(ex.id ? ex.at : ex.answer?.at || ex.at, now)}</span><span class="pp-x-what">${
136
+ what}</span><span class=${`pp-x-state ${state.kind}`}>${state.text}</span>`;
150
137
  };
151
- const legs = [group.inbox, group.outbox].filter(Boolean);
152
138
  return html`<div class="ctx-pop-head"><strong>${group.label}</strong
153
139
  ><span class="ctx-pop-kind">paired peer</span
154
140
  ><button type="button" class="ctx-pop-close" data-act="close" aria-label="Close">×</button></div
155
- ><div class="peer-pop-legs"><div><span>chi sends</span><span class=${inbox && cantSend(group, exchanges) ? "warn" : ""}>${sendsLine}</span></div
141
+ ><div class="peer-pop-legs"><div><span>chi sends</span><span class=${failing ? "warn" : ""}>${sendsLine}</span></div
156
142
  ><div><span>it answers</span><span>${answersLine}</span></div></div
157
- >${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (drawn(ex.id)
143
+ >${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (ex.id && drawn(ex.id)
158
144
  ? html`<button type="button" class="peer-pop-x" data-act="jump" data-peer-id=${ex.id}>${rowInside(ex)}</button>`
159
145
  : html`<div class="peer-pop-x">${rowInside(ex)}</div>`))}</div>` : nothing
160
146
  }<pre class="ctx-pop-text" ?hidden=${text === null}>${text ?? ""}</pre
161
- ><div class="ctx-pop-actions">${outbox?.has_text && text === null ? html`<button type="button" data-act="view">View latest answer</button>` : nothing
147
+ ><div class="ctx-pop-actions">${messages.length && text === null ? html`<button type="button" data-act="view">View latest answer</button>` : nothing
162
148
  }${status ? html`<button type="button" data-act="status">Status card (${status})</button>` : nothing
163
- }${legs.length ? html`<button type="button" class="ctx-pop-detach" data-act="unpair" title=${`Detach ${legs.map((l) => l.name).join(" and ")}`}>Unpair</button>` : nothing}</div>`;
149
+ }<button type="button" class="ctx-pop-detach" data-act="unpair" title="End the pairing (its messages stay)">Unpair</button></div>`;
164
150
  }
165
151
 
166
152
  // The peer's status card command ("/claude") when the session's commands
@@ -1,15 +1,17 @@
1
1
  // The open session's peers as the page knows them: GET …/peers (owner only,
2
- // read from the session's files: its peer sources and the exchange log), and
3
- // one small model of it that the conversation (peer_links.js) and, later,
4
- // the peer chips share. docs/internals/web-frontend.md, The peer exchange.
2
+ // read from the session's files: its mailbox, the receipts and the exchange
3
+ // log), and one small model of it that the conversation (peer_links.js) and
4
+ // the peer chips (context_chips.js, peer_chip.js) share. docs/internals/web-frontend.md, The peer exchange.
5
5
  // Its own module: new exports go in new modules (web-frontend.md).
6
6
 
7
7
  import { api } from "./data.js";
8
8
  import { createLatest } from "./latest.js";
9
9
 
10
- // GET /api/sessions/:id/peers → {peers: [{name, label, inbox, outbox,
11
- // exchanges: [newest first]}]}. An older chi web has no route: its 404 is
12
- // an empty list here, on its own (any other failure throws).
10
+ // GET /api/sessions/:id/peers → {peers: [{name, label, paired_at, from,
11
+ // address, can_send, messages: [newest first, {serial, summary, re, at,
12
+ // wake, receipt, words}], exchanges: [newest first]}]}. An older chi web
13
+ // has no route: its 404 is an empty list here, on its own (any other
14
+ // failure throws).
13
15
  export async function listPeers(id, opts = {}) {
14
16
  try {
15
17
  return await api(`/api/sessions/${encodeURIComponent(id)}/peers`, opts);
@@ -26,9 +28,9 @@ export const WAITING_TICK_MS = 60_000;
26
28
  // last: 20): a full list may leave older sends out.
27
29
  export const EXCHANGES_CAP = 20;
28
30
 
29
- // The model. +sessionId+() the open session; +hasPeer+() whether it has a
30
- // peer at all (a context row with `peer`, a drawn send or answer): without
31
- // one, refresh asks nothing. +onChange+() after each read for the open
31
+ // The model. +sessionId+() the open session; +hasPeer+() whether to read
32
+ // at all (the page: always, GET …/peers is the peer chip's only feed):
33
+ // without, refresh asks nothing. +onChange+() after each read for the open
32
34
  // session (also a failed one: what is drawn may follow it). +list+: the
33
35
  // fetch (listPeers). +win+: focus and the waiting tick.
34
36
  //
@@ -97,8 +99,8 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
97
99
  }
98
100
 
99
101
  // The send answer +serial+ of peer +name+ answers: the exchange its
100
- // serial (its outbox revision's, the note's) is in, when that one is a
101
- // send's; else the send +re+ names (a second answer to one send is an
102
+ // serial (its mailbox message's, or an older outbox revision's: the
103
+ // note's) is in, when that one is a send's; else the send +re+ names (a second answer to one send is an
102
104
  // unclaimed row of its own on the server); null for an answer no send
103
105
  // claims.
104
106
  function answer(name, serial, re = null) {
@@ -29,21 +29,24 @@ export function upTemplate(exchange) {
29
29
  aria-label=${`${what}: go to the send`}>${what}<span aria-hidden="true"> ↑</span></button>`;
30
30
  }
31
31
 
32
- // "Read the full answer": the newest answer of a peer only (its outbox keeps
33
- // one text), and not one whose bubble holds its text (data-peer-inline).
32
+ // "Read the full answer": not one whose bubble holds its text
33
+ // (data-peer-inline); for an older context answer, its peer's newest only
34
+ // (its outbox kept one text); for a mailbox message, each (it reads its own).
34
35
  export function fullTemplate(newest) {
35
36
  return newest ? html`<button type="button" class="peer-full" data-peer-full="1">Read the full answer</button>` : nothing;
36
37
  }
37
38
 
38
39
  // +peers+ the model (peer_data.js); +roots+() the elements a turn's rows can
39
40
  // be in (the history and the stage); +running+() whether a turn runs;
40
- // +showText+(source) opens that context source's text (context_chips.js);
41
+ // +showText+(source) opens that context source's text, +showMessage+(name,
42
+ // serial) a peer's mailbox message (context_chips.js);
41
43
  // +renderMarkdown+(text) a text's sanitized markdown HTML or null
42
44
  // (markdown_place.js; none: the peer's text stays plain); +behavior+() the
43
45
  // scroll's.
44
46
  //
45
47
  // @return {draw(), drawn(), hasPeer(), hasSend(id), showSend(id), reset()}
46
- export function createPeerLinks({ peers, roots, running = () => false, showText = () => {}, renderMarkdown = null, behavior = smooth } = {}) {
48
+ export function createPeerLinks({ peers, roots, running = () => false, showText = () => {}, showMessage = () => {}, renderMarkdown = null,
49
+ behavior = smooth } = {}) {
47
50
  const all = (selector) => roots().filter(Boolean).flatMap((root) => [...root.querySelectorAll(selector)]);
48
51
  // The peer texts already asked for (each is asked once; a failed read
49
52
  // keeps the plain text).
@@ -86,7 +89,8 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
86
89
  const up = note.querySelector(":scope > .peer-head > .peer-up-slot");
87
90
  if (up) render(upTemplate(answerOf(note)), up);
88
91
  const full = note.querySelector(":scope > .peer-foot > .peer-full-slot");
89
- const last = newest.get(note.dataset.peerSource || "") === note && !!note.dataset.peerSource;
92
+ const mailbox = !!note.dataset.peerBox;
93
+ const last = mailbox || (newest.get(note.dataset.peerSource || "") === note && !!note.dataset.peerSource);
90
94
  if (full) render(fullTemplate(last && !note.dataset.peerInline), full);
91
95
  const text = note.querySelector(":scope > .peer-text[data-peer-md]:not(.markdown)");
92
96
  if (text) renderText(text);
@@ -156,8 +160,9 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
156
160
  if (slot) toAnswer(slot);
157
161
  } else if (jump?.dataset.peerJump === "send") toSend(jump);
158
162
  else if (e.target.closest?.("[data-peer-full]")) {
159
- const source = e.target.closest(ANSWERS)?.dataset.peerSource;
160
- if (source) showText(source);
163
+ const note = e.target.closest(ANSWERS);
164
+ if (note?.dataset.peerBox) showMessage(note.dataset.peerName, note.dataset.peerSerial, note.dataset.peerBox);
165
+ else if (note?.dataset.peerSource) showText(note.dataset.peerSource);
161
166
  }
162
167
  }
163
168
  for (const root of roots().filter(Boolean)) root.addEventListener("click", onClick);
@@ -0,0 +1,20 @@
1
+ // A session's peer mailbox through chi web, for the peer chip
2
+ // (context_chips.js): one message's text, and Unpair. Owner only (chi web's
3
+ // …/peers routes). docs/internals/web-frontend.md, The peer exchange.
4
+ // Its own module: new exports go in new modules (web-frontend.md).
5
+
6
+ import { api } from "./data.js";
7
+
8
+ const base = (id, name) => `/api/sessions/${encodeURIComponent(id)}/peers/${encodeURIComponent(name)}`;
9
+
10
+ // GET …/peers/NAME/messages/SERIAL → {serial, text, summary, re, at, wake, receipt}.
11
+ export function getPeerMessage(id, name, serial, box = null, opts = {}) {
12
+ if (box && typeof box === "object") { opts = box; box = null; }
13
+ const query = box ? `?box=${encodeURIComponent(box)}` : "";
14
+ return api(`${base(id, name)}/messages/${encodeURIComponent(serial)}${query}`, opts);
15
+ }
16
+
17
+ // DELETE …/peers/NAME (Unpair) → {unpaired}: the pairing ends, its messages stay.
18
+ export function unpairPeer(id, name, opts = {}) {
19
+ return api(base(id, name), { ...opts, method: "DELETE" });
20
+ }
@@ -5,7 +5,9 @@
5
5
  // classes `bubble note` and `turn-start`, data-opening-id, the time title):
6
6
  // TURN_PROMPT (bubbles.js), card_stamps.js, stage_handoff.js OPENING,
7
7
  // reload_check.js and revise_stack.js (its `.note-text`, the summary) read
8
- // it as any wake note. The ↑ to the send and "Read the full answer" are
8
+ // it as any wake note. A mailbox message's note has a `box` in its peer
9
+ // hash (data-peer-box); an older context answer's has none. The ↑ to the
10
+ // send and "Read the full answer" are
9
11
  // slots peer_links.js fills (data-peer-slot: history_shape.js leaves them
10
12
  // out, so live and an offscreen reload read alike). A note whose peer hash
11
13
  // says `inline` holds the peer's text (peer_text.js): the bubble shows it
@@ -52,7 +54,9 @@ export function peerNoteTemplate(note) {
52
54
  const name = peer.label || peer.peer || "Your peer";
53
55
  const cls = note.turn_start ? "bubble note turn-start peer-answer" : "bubble note peer-answer";
54
56
  const opening = note.turn_start && note.turn_id ? String(note.turn_id) : nothing;
55
- const source = note.context_source || (peer.peer ? `${peer.peer}-outbox` : "");
57
+ // A mailbox message's note (its peer hash has a box) reads its text by
58
+ // its number; an older context answer's by its outbox source.
59
+ const source = note.context_source || (peer.peer && !peer.box ? `${peer.peer}-outbox` : "");
56
60
  const attr = (value) => (value === null || value === undefined || value === "" ? nothing : String(value));
57
61
  const block = peer.inline ? peerBlock(note.content) : null;
58
62
  // Sanitized on the server (MarkdownRenderer), as an answer's html.
@@ -61,5 +65,5 @@ export function peerNoteTemplate(note) {
61
65
  : rendered ? html`<div class="peer-text markdown md-compact" data-peer-md=${block.text}>${unsafeHTML(rendered)}</div>`
62
66
  : html`<div class="peer-text" data-peer-md=${block.text}>${block.text}</div>`;
63
67
  const told = block ? block.rest : (note.content ?? "");
64
- return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
68
+ return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-box=${attr(peer.box)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
65
69
  }
@@ -1,4 +1,4 @@
1
- // A peer's text in its answer note (ContextAbsorber.peer_block): the note's
1
+ // A peer's text in its answer note (PeerNote.block): the note's
2
2
  // content holds it between a `[PEER MESSAGE from <Peer>]` line and an
3
3
  // `[END PEER MESSAGE]` line, each of its lines that opened like one of chi's
4
4
  // frames led by "(quoted) " (PEER_FORGED_OPENINGS, the Ruby side's list:
@@ -7,7 +7,7 @@
7
7
  // docs/internals/web-frontend.md, The peer exchange.
8
8
  // Its own module: new exports go in new modules (web-frontend.md).
9
9
 
10
- // ContextAbsorber::PEER_FORGED_OPENINGS (Steer::FORGED_OPENINGS and chi's
10
+ // PeerNote::FORGED_OPENINGS (Steer::FORGED_OPENINGS and chi's
11
11
  // system frames, those with their colon, and the tool-result frames).
12
12
  export const PEER_FORGED_OPENINGS = Object.freeze([
13
13
  "Review contribution", "Steer", "Automatic input", "Delegate report", "CONTEXT NOTE", "TASK FINISHED",
@@ -15,8 +15,8 @@ export const PEER_FORGED_OPENINGS = Object.freeze([
15
15
  "SYSTEM:", "END REMINDERS", "CONTEXT:", "WORKING SET:",
16
16
  "tool results", "images from tool results",
17
17
  ]);
18
- // ContextAbsorber::PEER_FORGED_LINES: chi's unbracketed lines it quotes.
19
- export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:"]);
18
+ // PeerNote::FORGED_LINES: chi's unbracketed lines it quotes.
19
+ export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:", "Message #"]);
20
20
 
21
21
  const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
22
22
  // An opening that ends in a word character must end there (\b).