@gavin-hu/my-pi-agent 0.18.0 → 0.19.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 (249) hide show
  1. package/README.md +52 -44
  2. package/README.zh-CN.md +46 -41
  3. package/docs/extensions/README.md +34 -0
  4. package/docs/extensions/board.md +344 -0
  5. package/{extensions/bytefly/README.md → docs/extensions/bytefly.md} +446 -404
  6. package/{extensions/check/README.md → docs/extensions/check.md} +83 -68
  7. package/{extensions/context/README.md → docs/extensions/context.md} +40 -23
  8. package/docs/extensions/file-browse.md +554 -0
  9. package/{extensions/file-open/README.md → docs/extensions/file-open.md} +31 -23
  10. package/{extensions/file-read/README.md → docs/extensions/file-read.md} +47 -55
  11. package/{extensions/goal/README.md → docs/extensions/goal.md} +115 -131
  12. package/{extensions/job/README.md → docs/extensions/job.md} +61 -50
  13. package/{extensions/memory/README.md → docs/extensions/memory.md} +71 -60
  14. package/{extensions/plan/README.md → docs/extensions/plan.md} +390 -317
  15. package/{extensions/question/README.md → docs/extensions/question.md} +54 -31
  16. package/{extensions/rewind/README.md → docs/extensions/rewind.md} +115 -122
  17. package/docs/extensions/session-bus.md +285 -0
  18. package/{extensions/subagent/README.md → docs/extensions/subagent.md} +165 -118
  19. package/docs/extensions/team.md +226 -0
  20. package/{extensions/todo/README.md → docs/extensions/todo.md} +85 -88
  21. package/{extensions/watch/README.md → docs/extensions/watch.md} +54 -36
  22. package/{extensions/web-access/README.md → docs/extensions/web-access.md} +93 -50
  23. package/{extensions/wechat/README.md → docs/extensions/wechat.md} +258 -162
  24. package/{extensions/worktree/README.md → docs/extensions/worktree.md} +115 -72
  25. package/extensions/board/commands.ts +87 -0
  26. package/extensions/board/config.ts +36 -0
  27. package/extensions/board/format.ts +72 -0
  28. package/extensions/board/index.ts +40 -0
  29. package/extensions/board/input.ts +54 -0
  30. package/extensions/board/join.ts +42 -0
  31. package/extensions/board/page.ts +312 -0
  32. package/extensions/board/result.ts +99 -0
  33. package/extensions/board/rows.ts +48 -0
  34. package/extensions/board/schema.ts +75 -0
  35. package/extensions/board/seat.ts +29 -0
  36. package/extensions/board/snapshot.ts +41 -0
  37. package/extensions/board/tools.ts +279 -0
  38. package/extensions/board/transport/router.ts +172 -0
  39. package/extensions/board/transport/server.ts +149 -0
  40. package/extensions/board/types.ts +54 -0
  41. package/extensions/board/view/actions.ts +306 -0
  42. package/extensions/board/view/client.ts +41 -0
  43. package/extensions/board/view/keys.ts +121 -0
  44. package/extensions/board/view/live.ts +397 -0
  45. package/extensions/board/view/page.ts +158 -0
  46. package/extensions/board/view/render.ts +496 -0
  47. package/extensions/board/view/styles.ts +211 -0
  48. package/extensions/board/view/team.ts +305 -0
  49. package/extensions/board/writes.ts +99 -0
  50. package/extensions/bytefly/panel/commands.ts +40 -7
  51. package/extensions/bytefly/panel/settings-runtime.ts +0 -6
  52. package/extensions/bytefly/panel/settings.ts +3 -20
  53. package/extensions/bytefly/panel/tui/settings-view.ts +1 -1
  54. package/extensions/bytefly/picker/discovery.ts +0 -2
  55. package/extensions/bytefly/picker/types.ts +0 -2
  56. package/extensions/bytefly/update/check.ts +39 -0
  57. package/extensions/bytefly/update/format.ts +15 -0
  58. package/extensions/bytefly/update/register.ts +73 -34
  59. package/extensions/bytefly/update/state.ts +6 -3
  60. package/extensions/context/tui.ts +1 -1
  61. package/extensions/file-browse/{config.ts → config/config.ts} +1 -1
  62. package/extensions/file-browse/{files.ts → files/files.ts} +1 -14
  63. package/extensions/file-browse/{paths.ts → files/paths.ts} +1 -1
  64. package/extensions/file-browse/{git.ts → git/git.ts} +10 -3
  65. package/extensions/file-browse/index.ts +4 -4
  66. package/extensions/file-browse/{live.ts → live/live.ts} +20 -69
  67. package/extensions/file-browse/{router.ts → transport/router.ts} +21 -15
  68. package/extensions/file-browse/transport/server.ts +110 -0
  69. package/extensions/file-browse/{chrome.ts → view/chrome.ts} +4 -4
  70. package/extensions/file-browse/{client.ts → view/client.ts} +1 -1
  71. package/extensions/file-browse/{diff-html.ts → view/diff-html.ts} +3 -3
  72. package/extensions/file-browse/{html.ts → view/html.ts} +14 -12
  73. package/extensions/file-browse/{icons.ts → view/icons.ts} +1 -1
  74. package/extensions/file-browse/{markdown.ts → view/markdown.ts} +7 -7
  75. package/extensions/file-browse/{raw-html.ts → view/raw-html.ts} +1 -1
  76. package/extensions/file-browse/{styles.ts → view/styles.ts} +29 -5
  77. package/extensions/file-browse/{tree.ts → view/tree.ts} +1 -1
  78. package/extensions/file-browse/{urls.ts → view/urls.ts} +1 -1
  79. package/extensions/file-read/schema.ts +2 -1
  80. package/extensions/memory/store.ts +12 -5
  81. package/extensions/plan/blocks.ts +23 -2
  82. package/extensions/plan/commands.ts +21 -16
  83. package/extensions/plan/feedback.ts +147 -38
  84. package/extensions/plan/index.ts +6 -4
  85. package/extensions/plan/plan-index.ts +43 -9
  86. package/extensions/plan/plan-text.ts +33 -23
  87. package/extensions/plan/plans.ts +8 -11
  88. package/extensions/plan/policy.ts +20 -12
  89. package/extensions/plan/review.ts +3 -27
  90. package/extensions/plan/runtime.ts +21 -9
  91. package/extensions/plan/sections.ts +112 -0
  92. package/extensions/plan/steps.ts +3 -22
  93. package/extensions/plan/tools/exit.ts +11 -95
  94. package/extensions/plan/tui.ts +5 -34
  95. package/extensions/plan/types.ts +0 -8
  96. package/extensions/plan/web-client.ts +1 -1
  97. package/extensions/plan/web-page.ts +0 -7
  98. package/extensions/plan/web-router.ts +3 -57
  99. package/extensions/plan/web-share-session.ts +24 -30
  100. package/extensions/plan/web-streams.ts +14 -14
  101. package/extensions/plan/web-tokens.ts +2 -2
  102. package/extensions/plan/web.ts +66 -120
  103. package/extensions/rewind/{commands.ts → commands/commands.ts} +5 -5
  104. package/extensions/rewind/{review.ts → commands/review.ts} +13 -13
  105. package/extensions/rewind/{rewind.ts → commands/rewind.ts} +6 -6
  106. package/extensions/rewind/{config.ts → config/config.ts} +1 -1
  107. package/extensions/rewind/index.ts +3 -3
  108. package/extensions/rewind/{policy.ts → state/policy.ts} +4 -4
  109. package/extensions/rewind/{runtime.ts → state/runtime.ts} +11 -11
  110. package/extensions/rewind/{snapshot-run.ts → state/snapshot-run.ts} +1 -1
  111. package/extensions/rewind/{timeline.ts → state/timeline.ts} +2 -2
  112. package/extensions/rewind/{format.ts → text/format.ts} +3 -3
  113. package/extensions/rewind/{git.ts → transport/git.ts} +2 -2
  114. package/extensions/rewind/{restore.ts → transport/restore.ts} +2 -2
  115. package/extensions/rewind/{snapshot.ts → transport/snapshot.ts} +1 -1
  116. package/extensions/rewind/{store.ts → transport/store.ts} +1 -1
  117. package/extensions/rewind/{checklist.ts → view/checklist.ts} +6 -6
  118. package/extensions/rewind/{hunk-picker.ts → view/hunk-picker.ts} +8 -8
  119. package/extensions/rewind/{path-picker.ts → view/path-picker.ts} +7 -7
  120. package/extensions/rewind/{review-tui.ts → view/review-tui.ts} +11 -11
  121. package/extensions/rewind/{rewind-tui.ts → view/rewind-tui.ts} +17 -11
  122. package/extensions/rewind/{status.ts → view/status.ts} +4 -4
  123. package/extensions/session-bus/commands.ts +17 -69
  124. package/extensions/session-bus/format.ts +22 -45
  125. package/extensions/session-bus/index.ts +13 -7
  126. package/extensions/session-bus/join-request.ts +40 -0
  127. package/extensions/session-bus/mail-watch.ts +134 -0
  128. package/extensions/session-bus/notice.ts +32 -4
  129. package/{lib/claims → extensions/session-bus}/overlap.ts +11 -18
  130. package/extensions/session-bus/protocol.ts +19 -49
  131. package/extensions/session-bus/resolve.ts +67 -0
  132. package/extensions/session-bus/result.ts +16 -51
  133. package/extensions/session-bus/rows.ts +4 -2
  134. package/extensions/session-bus/runtime.ts +129 -238
  135. package/extensions/session-bus/store.ts +42 -6
  136. package/extensions/session-bus/tools.ts +31 -66
  137. package/extensions/session-bus/tui.ts +4 -4
  138. package/extensions/session-bus/types.ts +13 -13
  139. package/extensions/subagent/agents.ts +9 -8
  140. package/extensions/subagent/invocation.ts +4 -2
  141. package/extensions/team/commands.ts +112 -0
  142. package/extensions/team/dispatch.ts +133 -0
  143. package/extensions/team/format.ts +105 -0
  144. package/extensions/team/index.ts +33 -0
  145. package/extensions/team/schema.ts +29 -0
  146. package/extensions/team/tools.ts +246 -0
  147. package/extensions/team/types.ts +48 -0
  148. package/extensions/web-access/fetch/page.ts +4 -1
  149. package/extensions/web-access/fetch/structured.ts +447 -0
  150. package/extensions/web-access/fetch/tool.ts +2 -1
  151. package/extensions/web-access/search/tool.ts +2 -1
  152. package/extensions/wechat/bridge.ts +76 -92
  153. package/extensions/wechat/{commands.ts → commands/commands.ts} +26 -18
  154. package/extensions/wechat/{doctor.ts → commands/doctor.ts} +5 -5
  155. package/extensions/wechat/{history.ts → commands/history.ts} +1 -1
  156. package/extensions/wechat/{login.ts → commands/login.ts} +3 -3
  157. package/extensions/wechat/{slash.ts → commands/slash.ts} +38 -2
  158. package/extensions/wechat/dialog/channels.ts +136 -0
  159. package/extensions/wechat/{interaction.ts → dialog/interaction.ts} +37 -37
  160. package/extensions/wechat/{prompt.ts → dialog/prompt.ts} +2 -2
  161. package/extensions/wechat/{remote-ui.ts → dialog/remote-ui.ts} +1 -1
  162. package/extensions/wechat/index.ts +10 -10
  163. package/extensions/wechat/{admission.ts → peer/admission.ts} +2 -2
  164. package/extensions/wechat/{peer-session.ts → peer/peer-session.ts} +2 -2
  165. package/extensions/wechat/{peer-worker.ts → peer/peer-worker.ts} +100 -22
  166. package/extensions/wechat/{presence.ts → peer/presence.ts} +8 -7
  167. package/extensions/wechat/{reply-pipeline.ts → peer/reply-pipeline.ts} +1 -1
  168. package/extensions/wechat/{credentials.ts → state/credentials.ts} +2 -2
  169. package/extensions/wechat/{state.ts → state/state.ts} +14 -2
  170. package/extensions/wechat/{format.ts → text/format.ts} +3 -3
  171. package/extensions/wechat/{notices.ts → text/notices.ts} +8 -0
  172. package/extensions/wechat/tools.ts +29 -12
  173. package/extensions/wechat/{client.ts → transport/client.ts} +26 -2
  174. package/extensions/wechat/{deliver.ts → transport/deliver.ts} +72 -28
  175. package/extensions/wechat/{inbound.ts → transport/inbound.ts} +5 -5
  176. package/extensions/wechat/{media.ts → transport/media.ts} +11 -5
  177. package/extensions/wechat/transport/outbound.ts +142 -0
  178. package/extensions/wechat/{poll.ts → transport/poll.ts} +3 -3
  179. package/extensions/wechat/types.ts +10 -1
  180. package/extensions/wechat/{login-screen.ts → view/login-screen.ts} +4 -4
  181. package/extensions/wechat/{status-screen.ts → view/status-screen.ts} +31 -26
  182. package/extensions/worktree/guard.ts +3 -5
  183. package/lib/README.md +8 -6
  184. package/lib/board/card.ts +290 -0
  185. package/lib/board/links.ts +117 -0
  186. package/lib/board/page-pointer.ts +256 -0
  187. package/lib/board/round-text.ts +72 -0
  188. package/lib/board/round.ts +89 -0
  189. package/lib/board/rules.ts +304 -0
  190. package/lib/board/session.ts +105 -0
  191. package/lib/board/store.ts +310 -0
  192. package/lib/board/view.ts +150 -0
  193. package/lib/errors.ts +12 -4
  194. package/lib/format.ts +13 -0
  195. package/lib/http/guard.ts +112 -0
  196. package/lib/http/index.ts +16 -5
  197. package/lib/http/serve.ts +234 -0
  198. package/lib/http/server.ts +12 -10
  199. package/lib/http/sink.ts +110 -0
  200. package/lib/http/token.ts +38 -0
  201. package/lib/policy.ts +12 -2
  202. package/lib/requirements.ts +6 -7
  203. package/lib/session-label.ts +64 -0
  204. package/lib/session-roster.ts +130 -0
  205. package/lib/session-tool.ts +184 -0
  206. package/lib/tool-names.ts +25 -2
  207. package/lib/tui/glyphs.ts +2 -0
  208. package/lib/web/README.md +5 -2
  209. package/lib/web/components.ts +5 -0
  210. package/lib/web/runtime.ts +7 -1
  211. package/lib/web/tokens.ts +25 -2
  212. package/package.json +26 -14
  213. package/skills/next/SKILL.md +4 -3
  214. package/skills/project-docs/SKILL.md +133 -0
  215. package/skills/project-docs/references/audit.md +131 -0
  216. package/skills/project-docs/references/changelog.md +105 -0
  217. package/skills/project-docs/references/discovery.md +127 -0
  218. package/skills/project-docs/references/readme.md +80 -0
  219. package/skills/project-docs/references/release.md +84 -0
  220. package/skills/project-docs/references/roadmap.md +93 -0
  221. package/skills/project-docs/scripts/state.ts +316 -0
  222. package/skills/refactor/SKILL.md +100 -125
  223. package/skills/refactor/references/handoff.md +35 -0
  224. package/skills/refactor/references/signals.md +54 -0
  225. package/skills/refactor/scripts/inventory.ts +587 -0
  226. package/extensions/file-browse/README.md +0 -506
  227. package/extensions/file-browse/server.ts +0 -141
  228. package/extensions/plan/split.ts +0 -87
  229. package/extensions/session-bus/README.md +0 -247
  230. package/extensions/session-bus/team.ts +0 -102
  231. package/extensions/wechat/outbound.ts +0 -100
  232. package/lib/plans/sections.ts +0 -172
  233. package/lib/plans/setting.ts +0 -52
  234. package/lib/plans/split.ts +0 -215
  235. package/lib/plans/store.ts +0 -19
  236. package/lib/plans/team.ts +0 -151
  237. package/skills/changelog/SKILL.md +0 -148
  238. package/skills/parallel-dev/SKILL.md +0 -150
  239. /package/extensions/file-browse/{rev.ts → live/rev.ts} +0 -0
  240. /package/extensions/file-browse/{diff.ts → text/diff.ts} +0 -0
  241. /package/extensions/file-browse/{fuzzy.ts → text/fuzzy.ts} +0 -0
  242. /package/extensions/file-browse/{live-client.ts → view/live-client.ts} +0 -0
  243. /package/extensions/file-browse/{palette.ts → view/palette.ts} +0 -0
  244. /package/extensions/rewind/{patch.ts → text/patch.ts} +0 -0
  245. /package/extensions/rewind/{paths.ts → text/paths.ts} +0 -0
  246. /package/extensions/rewind/{queue.ts → transport/queue.ts} +0 -0
  247. /package/extensions/rewind/{temp-index.ts → transport/temp-index.ts} +0 -0
  248. /package/extensions/wechat/{lock.ts → state/lock.ts} +0 -0
  249. /package/extensions/wechat/{channel.ts → text/channel.ts} +0 -0
@@ -7,8 +7,15 @@
7
7
 
8
8
  import { sanitizeLine } from "../../lib/format.ts";
9
9
  import type { BusMessage, ClaimConflict, ClaimRecord, SessionStatus } from "./types.ts";
10
- /** A short id suffix, neutralized like every other peer-supplied field. */
11
- export function shortId(id: string): string {
10
+ /**
11
+ * The last few characters of a peer's id, as the transcript shows it.
12
+ *
13
+ * Neutralized like every other peer-supplied field, and deliberately not
14
+ * `lib/session-label.ts`'s `shortId`: that one keeps the id's alphanumerics to
15
+ * build a default *label*, while this one is the id's own tail, sanitized for a
16
+ * row.
17
+ */
18
+ export function idSuffix(id: string): string {
12
19
  return sanitizeLine(id.slice(-6));
13
20
  }
14
21
 
@@ -22,50 +29,35 @@ export function formatStatus(status: SessionStatus): string {
22
29
  const unread = session.unread > 0 ? ` · ${session.unread} unread` : "";
23
30
  const label = sanitizeLine(session.label);
24
31
  const cwd = sanitizeLine(session.cwd);
25
- return `- ${label} (${shortId(session.id)}) · ${cwd} · ${sanitizeLine(session.mode)}${unread}`;
32
+ return `- ${label} (${idSuffix(session.id)}) · ${cwd} · ${sanitizeLine(session.mode)}${unread}`;
26
33
  });
27
34
  const claims = status.claims.map((claim) => `- ${claimLine(claim)}`);
28
35
  const alerts = status.watching ? [`Alerts: inbox watch on${status.watching.wake ? " (wake)" : ""}`] : [];
29
- const units = status.units ? [unitLine(status.units)] : [];
30
36
  return [
31
37
  `Sessions (${status.sessions.length})`,
32
38
  ...(sessions.length > 0 ? sessions : ["- none"]),
33
39
  "",
34
40
  `Claims (${status.claims.length})`,
35
41
  ...(claims.length > 0 ? claims : ["- none"]),
36
- ...(units.length > 0 ? ["", ...units] : []),
37
42
  ...(alerts.length > 0 ? ["", ...alerts] : []),
38
43
  "",
39
- `You: ${sanitizeLine(status.me.label)} (${shortId(status.me.id)})`,
44
+ `You: ${sanitizeLine(status.me.label)} (${idSuffix(status.me.id)})${status.onBus ? "" : " \u00b7 not on the bus yet \u2014 `session join` puts you on it"}`,
40
45
  ].join("\n");
41
46
  }
42
47
 
43
48
  /**
44
- * The newest split in this repository: joined over total, then each unit with
45
- * its holder. Peer-supplied labels are sanitized like every other field.
49
+ * The join result: the label peers address, and whether mail will reach this session.
50
+ *
51
+ * One sentence for one act, because `join` is one action — the name and the watch are
52
+ * two facts about the same decision, and a reader that had to ask twice would be reading
53
+ * a bus the user is not supposed to operate.
46
54
  */
47
- function unitLine(units: NonNullable<SessionStatus["units"]>): string {
48
- const rows = units.rows.map(
49
- (row) => `${sanitizeLine(row.unit)}${row.lead ? " (lead)" : ""} · ${row.holder ? sanitizeLine(row.holder) : "—"}`,
50
- );
51
- return [
52
- `Units ${units.joined}/${units.total} joined · ${sanitizeLine(units.team)}`,
53
- ...rows.map((row) => `- ${row}`),
54
- ].join("\n");
55
- }
56
-
57
- /** The rename result: the label the bus settled on, which peers now address. */
58
- export function formatName(label: string): string {
59
- return `This session is now named "${sanitizeLine(label)}".`;
60
- }
61
-
62
- /** The inbox-watch result. */
63
- export function formatWatch(watching: { armed: boolean; wake: boolean }, name?: string): string {
64
- const owner = name ? ` for "${sanitizeLine(name)}"` : "";
65
- if (!watching.armed) return `Inbox watch off${owner}.`;
55
+ export function formatJoin(label: string, watching: { armed: boolean; wake: boolean }): string {
56
+ const who = `On the bus as "${sanitizeLine(label)}"`;
57
+ if (!watching.armed) return `${who}, not listening: mail waits in the inbox.`;
66
58
  return watching.wake
67
- ? `Inbox watch on${owner}: a new message raises one turn.`
68
- : `Inbox watch on${owner}: new messages appear at your next turn.`;
59
+ ? `${who}, listening: a new message raises one turn.`
60
+ : `${who}, listening: new messages appear at your next turn.`;
69
61
  }
70
62
 
71
63
  /** One consumed inbox, oldest first. */
@@ -95,26 +87,11 @@ export function formatClaim(claimed: readonly string[], conflicts: readonly Clai
95
87
  if (claimed.length > 0) lines.push(`Claimed: ${claimed.map((path) => sanitizeLine(path)).join(", ")}.`);
96
88
  for (const conflict of conflicts) {
97
89
  const owner = sanitizeLine(conflict.ownerLabel);
98
- lines.push(`${sanitizeLine(conflict.path)} is owned by ${owner} (${shortId(conflict.owner)}).`);
90
+ lines.push(`${sanitizeLine(conflict.path)} is owned by ${owner} (${idSuffix(conflict.owner)}).`);
99
91
  }
100
92
  return lines.length > 0 ? lines.join("\n") : "Nothing claimed.";
101
93
  }
102
94
 
103
- /** Join result: which unit, and what it claimed or could not. */
104
- export function formatJoin(
105
- team: string,
106
- unit: string,
107
- claimed: readonly string[],
108
- conflicts: readonly ClaimConflict[],
109
- ): string {
110
- const where = `${sanitizeLine(team)}/${sanitizeLine(unit)}`;
111
- if (conflicts.length > 0) {
112
- return [`Could not join ${where}.`, ...formatClaim([], conflicts).split("\n").filter(Boolean)].join("\n");
113
- }
114
- const paths = claimed.map((path) => sanitizeLine(path)).join(", ");
115
- return claimed.length > 0 ? `Joined ${where}: claimed ${paths}.` : `Joined ${where}: nothing to claim.`;
116
- }
117
-
118
95
  /** Release result. */
119
96
  export function formatRelease(released: readonly string[], notOwned: readonly string[]): string {
120
97
  const lines: string[] = [];
@@ -1,13 +1,15 @@
1
1
  /**
2
2
  * session-bus — coordinate parallel Pi sessions on one repository.
3
3
  *
4
- * A terminal session joins on `session_start`; RPC, JSON, print, and WeChat peer
5
- * sessions join only when the tool or `/sessions` is used. The factory only
6
- * registers; the runtime owns the heartbeat and removes the registry entry on
7
- * shutdown. Nothing arms an inbox watch by itself, so delivery stays pull-based
8
- * until a session asks: with the watch armed, new mail is reported at the next
9
- * turn, or — with `wake` — raises one turn as soon as the agent is idle,
10
- * including right after a running turn settles.
4
+ * No session joins on start: it joins through `join`, through the first action
5
+ * that makes it addressable, or through `w` on its own board, in any mode.
6
+ * `/sessions` and `status` only read, so a fresh board is empty and a peer never
7
+ * appears merely because the package loaded. The factory only registers; the
8
+ * runtime owns the heartbeat and removes the registry entry on shutdown. Nothing
9
+ * arms an inbox watch by itself, so delivery stays pull-based until a session
10
+ * asks: with the watch armed, new mail is reported at the next turn, or — with
11
+ * `wake` — raises one turn as soon as the agent is idle, including right after a
12
+ * running turn settles.
11
13
  *
12
14
  * Load with: pi --extension ./extensions/session-bus
13
15
  */
@@ -17,6 +19,7 @@ import { isExtensionEnabled } from "../../lib/env.ts";
17
19
  import { messageOf } from "../../lib/errors.ts";
18
20
  import { createPendingReporter, registerPendingChannel } from "../../lib/report.ts";
19
21
  import { registerCommands } from "./commands.ts";
22
+ import { registerJoinRequests } from "./join-request.ts";
20
23
  import { createSessionBus, type SessionBus } from "./runtime.ts";
21
24
  import { registerTools } from "./tools.ts";
22
25
  import { mailNotice } from "./notice.ts";
@@ -42,6 +45,9 @@ export default function sessionBus(pi: ExtensionAPI, deps: SessionBusDeps = {}):
42
45
 
43
46
  registerTools(pi, bus);
44
47
  registerCommands(pi, bus);
48
+ // `board` and `team` join this session through one request: their commands cannot call
49
+ // a tool, and the user's surface for the team is theirs, not this extension's.
50
+ registerJoinRequests(pi, bus);
45
51
 
46
52
  registerPendingChannel(pi, {
47
53
  reporter,
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The join request, answered.
3
+ *
4
+ * `board` and `team` sit above the bus: `/board`, `/team join`, and a card write all
5
+ * put this session on the team, and none of them may import this extension. A *tool*
6
+ * context could reach the `session` tool and an `ExtensionCommandContext` cannot —
7
+ * the same limit `lib/plan-entry.ts` works around for plan mode — so the request
8
+ * travels on `pi.events` and this module answers it with the runtime's own `join`.
9
+ *
10
+ * The answer always comes: every path here ends in `respond`, including a failure, so
11
+ * a caller never waits on a listener that threw. The label also becomes Pi's session
12
+ * name, because a team label and the footer have to agree.
13
+ */
14
+
15
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
16
+ import { messageOf } from "../../lib/errors.ts";
17
+ import { type JoinResult, TEAM_JOIN_REQUEST, type TeamJoinRequest } from "../../lib/session-tool.ts";
18
+ import type { SessionBus } from "./runtime.ts";
19
+
20
+ export function registerJoinRequests(pi: ExtensionAPI, bus: SessionBus): void {
21
+ pi.events.on(TEAM_JOIN_REQUEST, (data) => {
22
+ const request = data as TeamJoinRequest | undefined;
23
+ if (!request || typeof request.respond !== "function") return;
24
+ void answer(request);
25
+ });
26
+
27
+ async function answer(request: TeamJoinRequest): Promise<void> {
28
+ let result: JoinResult;
29
+ try {
30
+ await bus.start(request.ctx);
31
+ const joined = await bus.join(request.input);
32
+ // One identity: the bus's label is also the name Pi's footer and `/resume` show.
33
+ if (request.input.label !== undefined) pi.setSessionName(joined.label);
34
+ result = { ok: true, label: joined.label, watching: joined.watching };
35
+ } catch (error) {
36
+ result = { ok: false, error: messageOf(error) };
37
+ }
38
+ request.respond(result);
39
+ }
40
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * The armed inbox watch: polling this session's own inbox and the queue of mail
3
+ * that arrived and has not been reported yet.
4
+ *
5
+ * It announces and never consumes: `inbox` keeps sole ownership of deleting mail,
6
+ * so a wake cannot eat a message the model did not read. Nothing here knows about
7
+ * the store, the repository, or the transcript — the runtime supplies one read-only
8
+ * peek and the timer, and the extension's reporter supplies `onWake`.
9
+ *
10
+ * Polling rather than `fs.watch`, like the `watch` extension's tail: one readdir on
11
+ * this session's own directory stands in for a platform event API.
12
+ */
13
+
14
+ import type { RepeatTimer } from "../../lib/timer.ts";
15
+ import type { BusMessage } from "./types.ts";
16
+
17
+ export interface MailWatchOptions {
18
+ /** This session's unread mail, read-only: nothing is consumed or marked. */
19
+ peek(): Promise<BusMessage[]>;
20
+ /** The repeating timer behind the poll. */
21
+ interval: RepeatTimer;
22
+ /** Poll interval; 0 disables the ticker (tests). */
23
+ watchMs: number;
24
+ }
25
+
26
+ /** Whether the watch is on, and whether it raises a turn. */
27
+ export interface WatchingState {
28
+ armed: boolean;
29
+ wake: boolean;
30
+ }
31
+
32
+ export interface MailWatch {
33
+ /** Arm or disarm this session's inbox watch. */
34
+ watch(input: { wake?: boolean; stop?: boolean }): Promise<WatchingState>;
35
+ /** The current state, for the status line. */
36
+ state(): WatchingState;
37
+ /** Whether the armed watch asked for a triggered turn; the settle retry reads it. */
38
+ wantsWake(): boolean;
39
+ /** Read the inbox without consuming it, queueing what is new; no-op when unarmed. */
40
+ poll(): Promise<void>;
41
+ /** Mail that arrived and has not been reported yet, oldest first. */
42
+ pending(): BusMessage[];
43
+ /** Drop mail from the pending queue, once reported or consumed. */
44
+ markReported(ids: readonly string[]): void;
45
+ /** Release the ticker and the queue, on shutdown. */
46
+ stop(): void;
47
+ set onWake(callback: (() => void) | undefined);
48
+ }
49
+
50
+ export function createMailWatch(options: MailWatchOptions): MailWatch {
51
+ const { peek, interval, watchMs } = options;
52
+
53
+ let armed = false;
54
+ let wake = false;
55
+ let stopTicker: (() => void) | undefined;
56
+ let pendingMail: BusMessage[] = [];
57
+ /** Ids already queued, reported, or consumed; a broadcast shares one id. */
58
+ let reported = new Set<string>();
59
+ let onWake: (() => void) | undefined;
60
+
61
+ function dropFromPending(ids: ReadonlySet<string>): void {
62
+ pendingMail = pendingMail.filter((message) => !ids.has(message.id));
63
+ }
64
+
65
+ function markReported(ids: readonly string[]): void {
66
+ for (const id of ids) reported.add(id);
67
+ dropFromPending(new Set(ids));
68
+ }
69
+
70
+ /**
71
+ * Queue what arrived since the last poll, once, and wake a turn when this
72
+ * session asked for one.
73
+ *
74
+ * The reported set is narrowed to the mail still on disk, so it is bounded by
75
+ * the unread count rather than by the session's whole history; an id is unique
76
+ * for good, so a message that was consumed can never be queued again.
77
+ */
78
+ async function pollOnce(): Promise<void> {
79
+ if (!armed) return;
80
+ const mail = await peek();
81
+ const unread = new Set(mail.map((message) => message.id));
82
+ reported = new Set([...reported].filter((id) => unread.has(id)));
83
+ // A message that expired or was consumed leaves the queue with its file.
84
+ pendingMail = pendingMail.filter((message) => unread.has(message.id));
85
+ const queued = new Set(pendingMail.map((message) => message.id));
86
+ const fresh = mail.filter((message) => !reported.has(message.id) && !queued.has(message.id));
87
+ if (fresh.length === 0) return;
88
+ pendingMail = [...pendingMail, ...fresh];
89
+ if (wake) onWake?.();
90
+ }
91
+
92
+ function disarm(): void {
93
+ stopTicker?.();
94
+ stopTicker = undefined;
95
+ armed = false;
96
+ wake = false;
97
+ pendingMail = [];
98
+ }
99
+
100
+ async function watch(input: { wake?: boolean; stop?: boolean }): Promise<WatchingState> {
101
+ if (input.stop) {
102
+ disarm();
103
+ return { armed: false, wake: false };
104
+ }
105
+ if (!armed) {
106
+ armed = true;
107
+ wake = input.wake === true;
108
+ await pollOnce();
109
+ if (watchMs > 0) stopTicker = interval(watchMs, () => void pollOnce().catch(() => {}));
110
+ return { armed, wake };
111
+ }
112
+ // The mode is a setting, not an arm-time flag: an explicit `wake` changes it
113
+ // under a queued notice, and an omitted one keeps it, so bare `watch` never
114
+ // demotes an armed session.
115
+ if (input.wake !== undefined) wake = input.wake;
116
+ // Asking for wake over mail already queued is a request for that burst too;
117
+ // mid-turn it is suppressed here and retried once the run settles.
118
+ if (wake && pendingMail.length > 0) onWake?.();
119
+ return { armed, wake };
120
+ }
121
+
122
+ return {
123
+ watch,
124
+ state: () => ({ armed, wake }),
125
+ wantsWake: () => armed && wake,
126
+ poll: pollOnce,
127
+ pending: () => [...pendingMail],
128
+ markReported,
129
+ stop: disarm,
130
+ set onWake(callback: (() => void) | undefined) {
131
+ onWake = callback;
132
+ },
133
+ };
134
+ }
@@ -20,10 +20,30 @@ const MAX_NOTICE_LINES = 5;
20
20
  /** A message body is clipped to this many characters in a preview. */
21
21
  const MAX_PREVIEW = 200;
22
22
 
23
+ /**
24
+ * A handoff is clipped far less generously, because it is an instruction and not a
25
+ * notification: the card, its paths, and the step to take all sit past the point
26
+ * where a message preview would have stopped. The inbox still holds the full text.
27
+ */
28
+ const HANDOFF_PREVIEW = 800;
29
+
30
+ /** What a waking turn is told to do with a handoff: work it, not acknowledge it. */
31
+ const HANDOFF_HINT =
32
+ "A handoff is work you now own: run `session inbox`, record the card's plan, and start — do not just acknowledge it.";
33
+
34
+ /** `1 handoff`, `2 new messages` — a burst of handoffs is named as the work it is. */
35
+ function headlineOf(messages: readonly BusMessage[]): string {
36
+ const count = messages.length;
37
+ const handoffs = messages.filter((message) => message.kind === "handoff").length;
38
+ if (handoffs === 0 || handoffs !== count) return `${count} new message${count === 1 ? "" : "s"}`;
39
+ return `${count} handoff${count === 1 ? "" : "s"}`;
40
+ }
41
+
23
42
  /** One message as a preview line: sender, kind, and the head of its text. */
24
43
  function previewOf(message: BusMessage): string {
25
44
  const text = sanitizeLine(message.text);
26
- const head = text.length > MAX_PREVIEW ? `${text.slice(0, MAX_PREVIEW)}…` : text;
45
+ const limit = message.kind === "handoff" ? HANDOFF_PREVIEW : MAX_PREVIEW;
46
+ const head = text.length > limit ? `${text.slice(0, limit)}…` : text;
27
47
  const paths = message.paths?.length ? ` (${message.paths.map((path) => sanitizeLine(path)).join(", ")})` : "";
28
48
  return `${sanitizeLine(message.fromLabel)}${SEPARATORS.item}${sanitizeLine(message.kind)}${SEPARATORS.item}${head}${paths}`;
29
49
  }
@@ -35,6 +55,11 @@ function previewOf(message: BusMessage): string {
35
55
  * message is passed to the renderer, whose own cap draws the `… N more` row. The
36
56
  * two caps are deliberately separate — one is a context budget, the other a
37
57
  * screen height.
58
+ *
59
+ * A `handoff` is the one kind that assigns work, so it is the one kind the text
60
+ * treats as an instruction rather than as mail: the headline counts handoffs, the
61
+ * preview keeps enough of the body to be actionable, and the hint says to start it.
62
+ * Nothing else about the kind changes — it still does not block or wait for a reply.
38
63
  */
39
64
  export function mailNotice(messages: BusMessage[]): NoticeMessage {
40
65
  const count = messages.length;
@@ -45,14 +70,17 @@ export function mailNotice(messages: BusMessage[]): NoticeMessage {
45
70
  detail: sanitizeLine(message.text).slice(0, MAX_PREVIEW),
46
71
  severity: message.kind === "event" ? "warn" : "info",
47
72
  }));
73
+ const headline = headlineOf(messages);
48
74
  return noticeMessage({
49
75
  content: burstText({
50
76
  marker: BUS_CONTEXT_MARKER,
51
- headline: `${count} new message${count === 1 ? "" : "s"}`,
77
+ headline,
52
78
  lines: shown.map((message) => `- ${previewOf(message)}`),
53
79
  ...(more > 0 ? { more: `… ${more} more` } : {}),
54
- hint: "Run `session inbox` to read and consume them.",
80
+ hint: messages.some((message) => message.kind === "handoff")
81
+ ? HANDOFF_HINT
82
+ : "Run `session inbox` to read and consume them.",
55
83
  }),
56
- details: { source: "session-bus", title: `${count} new message${count === 1 ? "" : "s"}`, items },
84
+ details: { source: "session-bus", title: headline, items },
57
85
  });
58
86
  }
@@ -1,20 +1,23 @@
1
1
  /**
2
2
  * Claim overlap (pure).
3
3
  *
4
- * Two things in this package need the same answer to "do these paths collide?":
5
- * `session-bus` when a session claims a path, and plan approval when a proposed
6
- * split names its units. They must agree, because a split that passes validation
7
- * and then conflicts at claim time — or the reverse — teaches the user that a
8
- * claim is arbitrary. So the predicate lives here once and both call it.
4
+ * The bus's one answer to "do these paths collide?": the runtime checks it before
5
+ * a claim is written, and the store checks it when a claim is read back. Two
6
+ * copies would let a claim succeed here and read back as a conflict there, which
7
+ * teaches the user that a claim is arbitrary.
8
+ *
9
+ * One export. `normalizeClaimPath` is the predicate's own step — a spellings rule
10
+ * with a single caller, inside this module — so a caller cannot normalize a path
11
+ * one way and compare it another.
9
12
  *
10
13
  * A claim on a directory covers every file below it, in either direction, and a
11
- * claim is always a repository-relative, posix-separated path: the caller
12
- * normalizes first, because a Windows spelling (`a\b`) and a posix one (`a/b`)
14
+ * claim is always a repository-relative, posix-separated path: both sides are
15
+ * normalized here, because a Windows spelling (`a\b`) and a posix one (`a/b`)
13
16
  * naming the same file must not read as two claims.
14
17
  */
15
18
 
16
19
  /** A repository-relative path with one separator and no trailing slash. */
17
- export function normalizeClaimPath(raw: string): string {
20
+ function normalizeClaimPath(raw: string): string {
18
21
  const posix = raw.trim().replace(/\\/g, "/");
19
22
  const trimmed = posix
20
23
  .replace(/^\.\//, "")
@@ -31,13 +34,3 @@ export function claimsConflict(a: string, b: string): boolean {
31
34
  if (left === "" || right === "") return true;
32
35
  return left === right || left.startsWith(`${right}/`) || right.startsWith(`${left}/`);
33
36
  }
34
-
35
- /** The first two prefixes in `paths` that collide, if any. */
36
- export function findClaimOverlap(paths: readonly string[]): [string, string] | undefined {
37
- for (let i = 0; i < paths.length; i++) {
38
- for (let j = i + 1; j < paths.length; j++) {
39
- if (claimsConflict(paths[i], paths[j])) return [paths[i], paths[j]];
40
- }
41
- }
42
- return undefined;
43
- }
@@ -6,63 +6,41 @@
6
6
  */
7
7
 
8
8
  import { isAbsolute, relative, resolve, sep } from "node:path";
9
- import { sanitizeLine } from "../../lib/format.ts";
9
+ import { MAX_MESSAGE_TEXT } from "../../lib/session-tool.ts";
10
+ import { SESSION_TOOL } from "../../lib/tool-names.ts";
10
11
 
11
- export const TOOL_NAME = "session";
12
+ /**
13
+ * The label rules live in `lib/` because a label is shared vocabulary: the board
14
+ * records one on a card it claims, and `session-bus` settles the one it is
15
+ * addressed by. Re-exported here so every module in this extension keeps
16
+ * importing its rules from the protocol they belong to.
17
+ */
18
+ export { defaultLabel, MAX_LABEL, normalizeLabel, shortId } from "../../lib/session-label.ts";
19
+
20
+ export const TOOL_NAME = SESSION_TOOL;
12
21
  export const COMMAND_NAME = "sessions";
13
- export const MAX_TEXT = 4_000;
22
+ /**
23
+ * The text cap, from `lib/session-tool.ts`, where the callers that clip to it live.
24
+ * Re-exported the way the label rules are, so this extension imports its protocol's
25
+ * constants from one place and two extensions cannot drift apart on the number.
26
+ */
27
+ export const MAX_TEXT = MAX_MESSAGE_TEXT;
14
28
  export const MAX_PATHS = 20;
15
- export const MAX_LABEL = 40;
16
29
  export const DEFAULT_LIMIT = 20;
17
30
  export const MAX_LIMIT = 50;
18
31
 
19
32
  export type SessionMode = "tui" | "rpc" | "json" | "print" | "unknown";
20
- export type SessionAction = "status" | "send" | "inbox" | "claim" | "release" | "join" | "leave" | "watch" | "name";
33
+ export type SessionAction = "status" | "send" | "inbox" | "claim" | "release" | "join";
21
34
  export type MessageKind = "message" | "event" | "ask" | "handoff";
22
35
 
23
- const ACTIONS: readonly SessionAction[] = [
24
- "status",
25
- "send",
26
- "inbox",
27
- "claim",
28
- "release",
29
- "join",
30
- "leave",
31
- "watch",
32
- "name",
33
- ];
36
+ const ACTIONS: readonly SessionAction[] = ["status", "send", "inbox", "claim", "release", "join"];
34
37
  const KINDS: readonly MessageKind[] = ["message", "event", "ask", "handoff"];
35
38
 
36
- /** Only a terminal session is visible on the bus before it asks to join. */
37
- export function isAutoJoinMode(mode: string | undefined): boolean {
38
- return mode === "tui";
39
- }
40
-
41
39
  /** The session mode as the record stores it. */
42
40
  export function sessionMode(mode: string | undefined): SessionMode {
43
41
  return mode === "tui" || mode === "rpc" || mode === "json" || mode === "print" ? mode : "unknown";
44
42
  }
45
43
 
46
- /** A safe label, used both for display and as a target. */
47
- export function normalizeLabel(raw: string | undefined, fallback: string): string {
48
- const text = sanitizeLine(raw ?? "")
49
- .replace(/\s+/g, " ")
50
- .trim();
51
- return (text || fallback).slice(0, MAX_LABEL);
52
- }
53
-
54
- /** A short, display-only suffix for a session id. */
55
- export function shortId(id: string): string {
56
- const clean = id.replace(/[^a-zA-Z0-9]/g, "");
57
- return (clean || id).slice(-6);
58
- }
59
-
60
- /** The automatic label before uniqueness is applied. */
61
- export function defaultLabel(sessionName: string | undefined, cwd: string, id: string): string {
62
- const base = normalizeLabel(sessionName, normalizeLabel(cwd.split(/[\\/]/).filter(Boolean).at(-1), "session"));
63
- return normalizeLabel(base, `session-${shortId(id)}`);
64
- }
65
-
66
44
  /** An action, or undefined when the model sent an unknown one. */
67
45
  export function normalizeAction(raw: unknown): SessionAction | undefined {
68
46
  return typeof raw === "string" && ACTIONS.includes(raw as SessionAction) ? (raw as SessionAction) : undefined;
@@ -105,14 +83,6 @@ export function normalizePaths(repoRoot: string, cwd: string, raw: unknown): { p
105
83
  return { paths };
106
84
  }
107
85
 
108
- /**
109
- * Whether one claim covers the other path in either direction.
110
- *
111
- * The predicate is `lib/claims/overlap.ts`, because plan approval validates a
112
- * split with the same question; a second copy here is how the two answers drift.
113
- */
114
- export { claimsConflict } from "../../lib/claims/overlap.ts";
115
-
116
86
  /** Resolve `to` to active sessions; `*` is broadcast and excludes the sender. */
117
87
  export function resolveRecipients(
118
88
  sessions: readonly { id: string; label: string }[],
@@ -0,0 +1,67 @@
1
+ /**
2
+ * A claim path as the store keeps it: repository-relative, posix, canonical.
3
+ *
4
+ * A path is data — it is resolved against the repository root, and canonicalised
5
+ * through its real target before it is compared or stored — so a symlinked
6
+ * spelling inside the repository and the file it names are one claim, and a path
7
+ * that leaves the repository is refused before anything is written.
8
+ *
9
+ * `lib/path.ts` owns the containment predicates; this owns the claim's shape.
10
+ */
11
+
12
+ import { isAbsolute, join, relative, sep } from "node:path";
13
+ import { isInside, realPathOfNearest } from "../../lib/path.ts";
14
+
15
+ /** A path this module declined, with the reason a caller can print. */
16
+ export interface ClaimPathError {
17
+ /** The path as the caller passed it, so a refusal names what it read. */
18
+ path: string;
19
+ /** Why it cannot be a claim. */
20
+ reason: string;
21
+ }
22
+
23
+ /** Every path resolved and deduplicated, or the first one refused. */
24
+ export type ResolvedClaimPaths = { paths: string[] } | { error: ClaimPathError };
25
+
26
+ /**
27
+ * The wording of the one refusal, shared so approval and the bus cannot phrase it
28
+ * two ways: the caller prefixes the subject it knows (a unit, or a session's call).
29
+ */
30
+ export function claimPathMessage(error: ClaimPathError): string {
31
+ return `path "${error.path}" ${error.reason}.`;
32
+ }
33
+
34
+ /**
35
+ * What the root itself resolves to, which is never a claim: it would cover every
36
+ * file in the repository, so a unit that lists it owns the repository.
37
+ */
38
+ const ROOT_ITSELF = "is the repository, not a path in it";
39
+
40
+ /** What a path outside the root resolves to. */
41
+ const OUTSIDE = "does not resolve to a file in the repository";
42
+
43
+ /** What an absolute path is refused for: it is not the claim's to place. */
44
+ const ABSOLUTE = "is absolute; a claim is relative to the repository root";
45
+
46
+ /**
47
+ * Resolve `paths` against `root`, or refuse the first one that is not in it.
48
+ *
49
+ * A path that does not exist yet is accepted: `realPathOfNearest` resolves the
50
+ * nearest existing ancestor, so a unit may name the directory its work creates.
51
+ */
52
+ export function resolveClaimPaths(root: string, paths: readonly string[]): ResolvedClaimPaths {
53
+ const realRoot = realPathOfNearest(root);
54
+ const canonical: string[] = [];
55
+ for (const path of paths) {
56
+ // `join` would re-root an absolute path under the repository, which is not what
57
+ // a caller meant: refuse it rather than claim a directory it never named.
58
+ if (isAbsolute(path)) return { error: { path, reason: ABSOLUTE } };
59
+ const target = realPathOfNearest(join(root, path));
60
+ const rel = relative(realRoot, target);
61
+ if (rel === "") return { error: { path, reason: ROOT_ITSELF } };
62
+ if (!isInside(realRoot, target)) return { error: { path, reason: OUTSIDE } };
63
+ const normalized = rel.split(sep).join("/");
64
+ if (!canonical.includes(normalized)) canonical.push(normalized);
65
+ }
66
+ return { paths: canonical };
67
+ }