@cmodjs/core 6.3.0 → 8.0.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 (138) hide show
  1. package/docs/commands.md +123 -65
  2. package/docs/dependencies.md +189 -4
  3. package/docs/index.md +56 -12
  4. package/docs/install-steps.md +20 -17
  5. package/docs/jobs.md +43 -16
  6. package/docs/mod.md +43 -27
  7. package/docs/options.md +2 -2
  8. package/docs/session.md +58 -32
  9. package/docs/testing.md +88 -24
  10. package/docs/ui.md +128 -58
  11. package/installer.d.ts +2 -2
  12. package/jobs/context.js +1 -1
  13. package/jobs/program.d.ts +4 -1
  14. package/jobs/program.d.ts.map +1 -1
  15. package/jobs/program.js +79 -20
  16. package/mod.d.ts +59 -9
  17. package/mod.d.ts.map +1 -1
  18. package/mod.js +5 -3
  19. package/package.json +1 -1
  20. package/records.d.ts +15 -2
  21. package/records.d.ts.map +1 -1
  22. package/records.js +24 -10
  23. package/register.d.ts.map +1 -1
  24. package/register.js +7 -4
  25. package/rpc.d.ts +7 -0
  26. package/rpc.d.ts.map +1 -0
  27. package/rpc.js +59 -0
  28. package/runtime/bound.d.ts +1 -0
  29. package/runtime/bound.d.ts.map +1 -1
  30. package/runtime/bound.js +10 -0
  31. package/runtime/bun.d.ts +20 -0
  32. package/runtime/bun.d.ts.map +1 -0
  33. package/runtime/bun.js +20 -0
  34. package/runtime/claude.d.ts +1 -0
  35. package/runtime/claude.d.ts.map +1 -1
  36. package/runtime/conversations.d.ts +12 -1
  37. package/runtime/conversations.d.ts.map +1 -1
  38. package/runtime/conversations.js +472 -362
  39. package/runtime/deadline.d.ts +4 -3
  40. package/runtime/deadline.d.ts.map +1 -1
  41. package/runtime/deadline.js +10 -4
  42. package/runtime/engine.d.ts +5 -4
  43. package/runtime/engine.d.ts.map +1 -1
  44. package/runtime/installer.d.ts +3 -3
  45. package/runtime/installer.d.ts.map +1 -1
  46. package/runtime/installer.js +10 -9
  47. package/runtime/interfaces.d.ts +262 -0
  48. package/runtime/interfaces.d.ts.map +1 -0
  49. package/runtime/interfaces.js +935 -0
  50. package/runtime/lifecycle.d.ts +3 -3
  51. package/runtime/lifecycle.d.ts.map +1 -1
  52. package/runtime/lifecycle.js +83 -52
  53. package/runtime/options.d.ts.map +1 -1
  54. package/runtime/options.js +2 -6
  55. package/runtime/parts.d.ts +64 -1
  56. package/runtime/parts.d.ts.map +1 -1
  57. package/runtime/parts.js +311 -4
  58. package/runtime/programs.d.ts +1 -0
  59. package/runtime/programs.d.ts.map +1 -1
  60. package/runtime/programs.js +5 -3
  61. package/runtime/rpc.d.ts +111 -0
  62. package/runtime/rpc.d.ts.map +1 -0
  63. package/runtime/rpc.js +387 -0
  64. package/runtime/sessions.d.ts +6 -4
  65. package/runtime/sessions.d.ts.map +1 -1
  66. package/runtime/sessions.js +59 -80
  67. package/runtime/toaster.js +1 -1
  68. package/runtime/ui.d.ts +23 -10
  69. package/runtime/ui.d.ts.map +1 -1
  70. package/runtime/ui.js +63 -52
  71. package/runtime/web.d.ts +0 -73
  72. package/runtime/web.d.ts.map +1 -1
  73. package/runtime/web.js +9 -5
  74. package/runtime/widgets.d.ts +4 -2
  75. package/runtime/widgets.d.ts.map +1 -1
  76. package/runtime/widgets.js +17 -16
  77. package/session/conversation.d.ts +7 -1
  78. package/session/conversation.d.ts.map +1 -1
  79. package/session/session.d.ts +31 -7
  80. package/session/session.d.ts.map +1 -1
  81. package/session/session.js +1 -0
  82. package/src/installer.ts +2 -2
  83. package/src/jobs/context.ts +1 -1
  84. package/src/jobs/program.ts +76 -21
  85. package/src/mod.ts +51 -10
  86. package/src/records.ts +33 -9
  87. package/src/register.ts +7 -4
  88. package/src/rpc.ts +67 -0
  89. package/src/runtime/bound.ts +10 -0
  90. package/src/runtime/bun.ts +40 -0
  91. package/src/runtime/claude.ts +1 -0
  92. package/src/runtime/conversations.ts +470 -352
  93. package/src/runtime/deadline.ts +13 -6
  94. package/src/runtime/engine.ts +5 -4
  95. package/src/runtime/installer.ts +13 -12
  96. package/src/runtime/interfaces.ts +1161 -0
  97. package/src/runtime/lifecycle.ts +102 -59
  98. package/src/runtime/options.ts +2 -5
  99. package/src/runtime/parts.ts +364 -5
  100. package/src/runtime/programs.ts +6 -3
  101. package/src/runtime/rpc.ts +457 -0
  102. package/src/runtime/sessions.ts +63 -83
  103. package/src/runtime/toaster.ts +1 -1
  104. package/src/runtime/ui.ts +82 -61
  105. package/src/runtime/web.ts +10 -39
  106. package/src/runtime/widgets.ts +20 -18
  107. package/src/session/conversation.ts +7 -1
  108. package/src/session/session.ts +22 -7
  109. package/src/testing/fake-claude.ts +38 -7
  110. package/src/testing/fake-serve.ts +144 -0
  111. package/src/testing/fake-sessions.ts +21 -6
  112. package/src/testing.ts +156 -68
  113. package/src/ui/areas.ts +2 -2
  114. package/src/ui/elements.ts +21 -16
  115. package/src/ui/panes.ts +14 -7
  116. package/testing/fake-claude.d.ts +6 -0
  117. package/testing/fake-claude.d.ts.map +1 -1
  118. package/testing/fake-claude.js +34 -7
  119. package/testing/fake-serve.d.ts +39 -0
  120. package/testing/fake-serve.d.ts.map +1 -0
  121. package/testing/fake-serve.js +110 -0
  122. package/testing/fake-sessions.d.ts.map +1 -1
  123. package/testing/fake-sessions.js +22 -6
  124. package/testing.d.ts +21 -4
  125. package/testing.d.ts.map +1 -1
  126. package/testing.js +138 -56
  127. package/ui/areas.d.ts +3 -1
  128. package/ui/areas.d.ts.map +1 -1
  129. package/ui/elements.d.ts +18 -9
  130. package/ui/elements.d.ts.map +1 -1
  131. package/ui/elements.js +9 -8
  132. package/ui/panes.d.ts +10 -6
  133. package/ui/panes.d.ts.map +1 -1
  134. package/ui/panes.js +5 -3
  135. package/runtime/pages.d.ts +0 -32
  136. package/runtime/pages.d.ts.map +0 -1
  137. package/runtime/pages.js +0 -142
  138. package/src/runtime/pages.ts +0 -183
package/docs/session.md CHANGED
@@ -8,7 +8,7 @@ import { Prompt, sessions } from '../node_modules/@cmodjs/core/mod.js'
8
8
  const prompts = (await sessions.current.messages()).filter((message) => message.type === Prompt)
9
9
  ```
10
10
 
11
- A mod hears, reads, and acts on the session it runs in and every other session on the computer.
11
+ A mod reads and acts on the session it runs in, every other session on the computer, and every session of another device a sessions provider serves to this one. It hears only the session it runs in, as a VS Code extension hears only its own window.
12
12
 
13
13
  ## The shape of a conversation
14
14
 
@@ -25,7 +25,7 @@ Session one agent and its record
25
25
  └── Message a prompt, a reply, a tool call, and the rest
26
26
  ```
27
27
 
28
- Every part of the record answers the same three calls: `search` finds its messages, `on` hears what happens in it, and `before` changes or refuses what is about to happen in it.
28
+ Every part of the record answers `search`, which finds its messages. Every part of the session the mod runs in also answers `on`, which hears what happens in it, and `before`, which changes or refuses what is about to happen in it.
29
29
 
30
30
  ## sessions
31
31
 
@@ -33,13 +33,14 @@ Every part of the record answers the same three calls: `search` finds its messag
33
33
  type Sessions = {
34
34
  readonly current: CurrentSession
35
35
  list(options?: { running?: boolean; since?: string; everyProject?: boolean }): Promise<Session[]>
36
+ get(id: string): Promise<Session>
36
37
  search(text: string, matcher?: Matcher<Message>, options?: { running?: boolean; since?: string; everyProject?: boolean; before?: Message }): AsyncIterable<Message>
37
- onMessage(matcher?, listener, options?): Subscription // and every on method below
38
38
  }
39
39
  ```
40
40
 
41
41
  - `current` is the session the mod runs in. It is there from `setup` on, and follows `/clear`, `/resume`, a fork, `/cd`, and a `cd` in a Bash or PowerShell call.
42
- - `list` lists the project's sessions, the most recent first. `everyProject` lists every project's, `running` keeps the sessions still open in a Claude Code window, and `since` keeps those active since a time, written as `30m`, `6h`, `2d`, `1w`, or a date such as `2026-10-01`.
42
+ - `list` lists the project's sessions, the most recent first. `everyProject` lists every project's, `running` keeps the sessions still open in a Claude Code window, and `since` keeps those active since a time, written as `30m`, `6h`, `2d`, `1w`, or a date such as `2026-10-01`. It lists this computer's sessions and those of every sessions provider a mod's program runs ([Sessions providers](#sessions-providers)).
43
+ - `get` finds one session by its `id`, running or ended, on this computer or through any sessions provider, and rejects with `No session <id> on this computer.` when none holds it.
43
44
  - `search` yields the messages whose text contains `text`, any case, newest first, in the project's sessions or, with `everyProject`, every project's. `text` is at least 3 characters. It reads the index a page at a time as the loop asks for more, and `break` stops it. A matcher keeps the messages it matches, and `before` starts after a message, so a "load more" picks up where a loop broke. A tool call is searched by its name and the first 1,024 characters of each input value, an instruction file by its path and text, and a tool result is not searched.
44
45
 
45
46
  ```ts
@@ -51,9 +52,15 @@ for await (const message of sessions.search('deploy job', { type: Prompt }, { si
51
52
  const more = sessions.search('deploy job', { type: Prompt }, { since: '1w', before: found.at(-1) })
52
53
  ```
53
54
 
54
- - Its `on` methods hear every running session of the project, this one included ([Listen](#listen-to-a-record)). `sessions.current`'s methods hear this session. `sessions` has no `before` methods. To change what happens in another session, call a `before` method on that session ([Change what happens](#change-what-happens)).
55
+ - `sessions` and a listed session have no `on` and no `before` methods, because a mod hears only the session it runs in. `sessions.current` and its parts have them ([Listen](#listen-to-a-record)).
55
56
 
56
- While cmod has set up a mod other than cmod, the cmod plugin indexes every conversation in the background at each session's start, so `search` waits only for what changed since. The `cmod history` command answers the same questions in a terminal ([commands.md](commands.md#cmod-history)).
57
+ While cmod has set up a mod other than cmod, the cmod plugin indexes every conversation in the background at each session's start, so `search` waits only for what changed since. The `cmod sessions` command answers the same questions in a terminal ([commands.md](commands.md#cmod-sessions)), and a script `cmod run` runs reads and drives sessions with the same `sessions` ([commands.md](commands.md#cmod-run)).
58
+
59
+ ### Sessions providers
60
+
61
+ One `cmod serve` per computer serves its sessions to every session and script on it ([commands.md](commands.md#cmod-serve)). When a mod's program provides `cmod/Devices` ([dependencies.md](dependencies.md#cmod-devices-providers-on-your-other-devices)), `cmod serve` reaches the `cmod serve` of each device it names, and that `cmod serve` provides `cmod/Sessions` for its device's sessions. A mod whose program provides the `cmod/Sessions` interface ([dependencies.md](dependencies.md#interfaces-several-mods-answer-one-name)) serves the sessions of other devices the same way. So `list`, `get`, and `search` read every device's sessions, and every action reaches them, as they reach this computer's. A call on one session goes to each provider in turn, `cmod serve` first, and the first that holds the session answers it. A provider that does not hold the session throws `NotHeld` from `rpc.js`, and a call no provider holds rejects with the last provider's message, such as `No session <id> on this computer.` or `This provider holds no session <id>.`
62
+
63
+ A provider answers each session it lists with `device: { id, name }`, the computer the session runs on, in the shape `device.read()` answers ([mod.md](mod.md#device-the-computer-the-session-runs-on)). `cmod serve` names this computer, and a provider a mod's program runs names the device each of its sessions runs on. A session of a computer without a machine id has no `device`.
57
64
 
58
65
  ## Session
59
66
 
@@ -70,6 +77,12 @@ type Session = {
70
77
  readonly startedAt: string
71
78
  readonly lastAt: string
72
79
  readonly forkedFrom?: string
80
+ readonly visitedAt?: string
81
+ readonly device?: { id: string; name: string }
82
+ readonly status?: 'busy' | 'shell' | 'idle' | 'waiting'
83
+ readonly waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }
84
+ readonly contextPercent?: number
85
+ readonly lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string }
73
86
  readonly goal: Goal
74
87
  context(): Promise<Context>
75
88
  contexts(): Promise<Context[]>
@@ -77,25 +90,32 @@ type Session = {
77
90
  turns(options?: { last?: number }): Promise<Turn[]>
78
91
  messages(options?: { after?: string; last?: number }): Promise<Message[]>
79
92
  search(text: string, matcher?: Matcher<Message>): AsyncIterable<Message>
80
- onMessage(matcher?, listener, options?): Subscription // and every on method below
81
- beforePrompt(matcher?, listener, options?): Subscription // and every before method below
82
93
  send(message: { type: Prompt | Note; text: string; as?: Person | Session }): Promise<void>
83
- startTask(task: { prompt: string; title: string; agentType?: string; model?: string; name?: string; cwd?: string; isBackground?: boolean }): Promise<Task>
84
- fork(task: { prompt: string; title: string }): Promise<Task>
85
- clone(task: { prompt: string; title: string }): Promise<Task>
94
+ startTask(task: { prompt: string; title: string; agentType?: string; model?: string; name?: string; cwd?: string; isBackground?: boolean; device?: string }): Promise<Task>
95
+ fork(task: { prompt: string; title: string; isBackground?: boolean; at?: string }): Promise<Task>
86
96
  callTool(call: { name: string; input: Record<string, unknown> }): Promise<ToolResult>
87
97
  usage(): Promise<Usage>
88
98
  surfaces(): Promise<Surface[]>
99
+ end(): Promise<void>
100
+ visit(at?: string): Promise<void>
101
+ delete(): Promise<void>
89
102
  }
90
103
  ```
91
104
 
92
- - `id` is Claude Code's session id, and a task owner's `id` is its task's id. `title` is the name the person gave it with `/rename`, or the first line of its first prompt. A resumed or forked session with no name, and a session the mod started in after its first prompt, has an empty `sessions.current.title` until `sessions.list()` reads it.
105
+ - `id` is Claude Code's session id, and a task owner's `id` is its task's id. `title` is the name the person gave it with `/rename`, or the first line of its first prompt. A resumed or forked session with no name, and a session the mod started in after its first prompt, has an empty `sessions.current.title` until `sessions.list()` reads it. `sessions.current.device` is empty until `sessions.list()` reads the current session from `cmod serve`, which names this computer, and holds from then on.
93
106
  - `color` is the color the person last picked with `/color`, such as `cyan`, and is absent when they never picked one or reset it with `/color default`. A task's owner has none. `sessions.current.color` follows each `/color` the person runs, and a resumed session's color holds from the mod's start, as `title` does.
94
107
  - `project` is the project the session works in, and `cwd` is its working folder. For the current session both follow `/cd`, and `cwd` follows a `cd` in a Bash or PowerShell call.
95
108
  - `agentType` is the agent definition the session runs, such as `Explore` for a task's owner, or the agent a session started with `claude --agent` runs. A plain session has none.
96
109
  - `model` is the model the session runs on. `startedAt` and `lastAt` are ISO times.
97
- - `forkedFrom` names the session a `/branch` or a fork copied it from, and for a task's owner the session whose `fork` or `clone` started it.
110
+ - `forkedFrom` names the session a `/branch` or a fork copied it from, and for a task's owner the session whose `fork` started it.
111
+ - `visitedAt` is when the person last opened the session's reply, in the browser app or in a mod that calls `visit`, as an ISO time. A reply after it is unread. A session nobody visited has none.
98
112
  - `isRunning` is true while the session is open in a Claude Code window, and for a task's owner while its task runs.
113
+ - `device` is the computer the session runs on, its `id` and `name` as `device.read()` answers them ([mod.md](mod.md#device-the-computer-the-session-runs-on)). A listed session's is the one its sessions provider names ([Sessions providers](#sessions-providers)). A session of a computer without a machine id has none, and so does a task's `owner`. `sessions.get(task.id)` reads a background task's session with the device its store names.
114
+ - `status` is what a running session does now, as Claude Code records it: `busy` while a turn runs, `waiting` while a dialog waits on the person, `shell` while a command a stopped turn left runs in its terminal, and `idle` between turns. A session whose Claude Code records none has the one its cmod plugin hears: `busy` from a turn's start to its end, `waiting` while a permission prompt or a question is open, else `idle`. An ended session, a task's `owner`, and `sessions.current` have none.
115
+ - `waitingFor` is what a `waiting` session waits on. `dialog` is Claude Code's name for it, such as `permission prompt`, `input needed`, or `dialog open`. A permission prompt for a tool call adds the call's `tool` and `input`, and an `AskUserQuestion` call adds its first `question`.
116
+ - `contextPercent` is how full the session's context window is, in percent, from its `usage()` after each turn. A session before its first answer has none.
117
+ - `lastTurn` is how the session's last main turn ended, as its cmod plugin hears it: `answered`, `stopped` when the person stopped it with Esc or an app's `abort`, or `failed` with the `failure` text Claude Code gave, and `at`, the ISO time it ended. A turn stopped with Esc has no ending in its transcript until the next prompt, so only the cmod plugin's hooks hear it. A running turn, a session whose cmod plugin has heard no turn end since it started, and an ended session have none.
118
+ - `cmod/Sessions` tells a `Change` of `part: 'status'` whenever a session's `status`, `waitingFor`, `contextPercent`, or `lastTurn` changes.
99
119
 
100
120
  ### Read a record
101
121
 
@@ -113,7 +133,7 @@ const explorers = (await sessions.current.goal.tasks()).filter((task) => task.ow
113
133
 
114
134
  A session that has ended keeps its record, so every read works on it.
115
135
 
116
- `sessions.current` adds two members for the rows Claude Code draws, `scrollTo(message, options?)` and `caption(toolCall, text)` ([ui.md](ui.md#the-conversations-rows)).
136
+ `sessions.current` adds two members for the rows Claude Code draws, `scrollTo(message, options?)` and `caption(toolCall, text)` ([ui.md](ui.md#the-conversation-s-rows)).
117
137
 
118
138
  ### Listen to a record
119
139
 
@@ -122,19 +142,21 @@ part.on<Event>(listener: (payload, time: { deadline: number; signal: AbortSignal
122
142
  part.on<Event>(matcher: Matcher<payload>, listener, options?): Subscription
123
143
  ```
124
144
 
125
- Each event has its own method, such as `onReply` or `onTaskStart`, which runs `listener` for each new message, turn, context, task, goal change, or usage change, as it happens. `sessions`, a session, its goal, a task, a context, and a turn each have every `on` method, and each hears what happens inside it: an event reaches the part it happens in and every part above it.
145
+ Each event has its own method, such as `onReply` or `onTaskStart`, which runs `listener` for each new message, turn, context, task, goal change, or usage change, as it happens. `sessions.current`, its goal, a task, a task's owner, a context, and a turn each have every `on` method, and each hears what happens inside it: an event reaches the part it happens in and every part above it.
146
+
147
+ A mod hears only the session it runs in, as a VS Code extension hears only its own window. A listed session has no `on` method, and a listener on a part of another session, such as a background task's owner or a turn read from another session, throws `<mod>: onReply works only in a mod's own session.`, as every listener of a `cmod run` script does. A background task's own `onTaskEnd` still hears it end, because the task belongs to this session's goal.
126
148
 
127
149
  A session is one agent: the conversation's main agent, or the agent doing a task. An event runs up one chain:
128
150
 
129
151
  ```
130
- Turn ──> Context ──> Session ──> that session's Task ──> each Task above ──> Goal ──> sessions
152
+ Turn ──> Context ──> Session ──> that session's Task ──> each Task above ──> Goal
131
153
  ```
132
154
 
133
155
  The goal belongs to the main agent, so the main agent's events reach it too. So:
134
156
  - `sessions.current.onToolCall(…)` hears the main agent's tool calls only.
135
157
  - `sessions.current.goal.onToolCall(…)` hears every agent's in the session.
136
158
  - `task.onMessage(…)` hears every message of the task's owner and of the tasks it handed on, and `task.owner.onMessage(…)` hears the owner's alone.
137
- - `turn.onReply(…)` hears the replies of that turn only, and `sessions.onPrompt(…)` hears every prompt of every running session in the project.
159
+ - `turn.onReply(…)` hears the replies of that turn only.
138
160
 
139
161
  A turn or a context that has ended takes no listener: its methods throw `<mod>: turn <id> has ended, so its onReply never fires. Listen on its Session.`
140
162
 
@@ -148,7 +170,7 @@ A turn or a context that has ended takes no listener: its methods throw `<mod>:
148
170
  | `onGoalChange` | The `Goal` with its new text. | The goal |
149
171
  | `onUsageChange` | The session's new `Usage`. | The session |
150
172
 
151
- The current session's events arrive as Claude Code writes them, and another running session's within a moment.
173
+ The events arrive as Claude Code writes them.
152
174
 
153
175
  A matcher keeps the payloads it matches. It is a partial copy of the payload: a key it names must match, and a key it leaves out matches anything. A string matches the same string, a `RegExp` matches the text it tests true on, and a list matches any one of its items. A key the payload lacks matches nothing.
154
176
 
@@ -173,7 +195,7 @@ sessions.current.onTurnEnd(async (turn, { signal }) => {
173
195
 
174
196
  A cancel stops the listener quietly. Claude Code stops the programs its `process.run` and `process.spawn` calls started, cmod goes on without its answer, and nothing it throws shows a toast.
175
197
 
176
- An `on` listener that passes its time stops quietly. A `before` listener that passes its time shows the error toast, and what was about to happen goes on as it was. With `onFailure: 'refuse'`, a `before` listener that throws or passes its time refuses instead, with the error as the reason, for a gate that must hold even when it breaks. The gate holds when Claude Code stops the mod's hook too, as it does when the listener's own code runs past 10 seconds without waiting on a call, and the toast `Claude Code stopped its <hook> hook` says so. On another session, it holds when that session does not answer within 5 seconds, or within what is left of the 8 seconds cmod waits on other sessions for one event, for what the listener's matcher and part cover. A `beforePermissionRequest` listener always refuses when it fails.
198
+ An `on` listener that passes its time stops quietly. A `before` listener that passes its time shows the error toast, and what was about to happen goes on as it was. With `onFailure: 'refuse'`, a `before` listener that throws or passes its time refuses instead, with the error as the reason, for a gate that must hold even when it breaks. The gate holds when Claude Code stops the mod's hook too, as it does when the listener's own code runs past 10 seconds without waiting on a call, and the toast `Claude Code stopped its <hook> hook` says so. A `beforePermissionRequest` listener always refuses when it fails.
177
199
 
178
200
  ### Change what happens
179
201
 
@@ -182,7 +204,7 @@ part.before<Event>(listener: (payload, time) => Answer | void, options?: { timeo
182
204
  part.before<Event>(matcher: Matcher<payload>, listener, options?): Subscription
183
205
  ```
184
206
 
185
- Each `before` method, such as `beforeToolCall`, runs `listener` just before something happens, and its answer changes or refuses it. Returning nothing lets it happen as it was. Each part except `sessions` has every `before` method, and hears what is about to happen inside it, the way `on` methods do.
207
+ Each `before` method, such as `beforeToolCall`, runs `listener` just before something happens, and its answer changes or refuses it. Returning nothing lets it happen as it was. Each part that has `on` methods has every `before` method, and hears what is about to happen inside it, the way `on` methods do.
186
208
 
187
209
  | Method | The listener can answer | Covers |
188
210
  | --- | --- | --- |
@@ -194,7 +216,7 @@ Each `before` method, such as `beforeToolCall`, runs `listener` just before some
194
216
  | `beforeReply`, `beforeNotice` | `{ text }` | The text the transcript keeps |
195
217
  | `beforeToolResult` | `{ output?, isError?, note? }` | The result, after the tool ran. Here `output` is what the tool answered Claude Code, such as `{ stdout, stderr }` for Bash, and an `output` answer replaces it in the same shape, `note` adds text beside it, and `isError: true` makes Claude read it as a failure, with the note as the reason. A failed call takes only a note |
196
218
  | `beforeToolCall` | `{ input?, note? }`, `{ result, note? }`, or `{ refuse }` | A tool call. `result` answers it without running the tool, and `note` reaches Claude with the call's result |
197
- | `beforeTaskStart` | `{ prompt?, title?, owner?: { agentType?, model?, cwd? }, isAsync? }` or `{ refuse }` | A task about to start. `prompt` and `title` replace what Claude wrote, `owner` picks the agent that does it, its model, and its folder, and `isAsync` runs a subagent while Claude goes on. A background task takes `prompt`, `title`, `owner`, and `refuse`, a fork and a clone take `prompt`, `title`, and `refuse`, and a workflow's agent takes only `refuse` |
219
+ | `beforeTaskStart` | `{ prompt?, title?, owner?: { agentType?, model?, cwd? }, isAsync? }` or `{ refuse }` | A task about to start. `prompt` and `title` replace what Claude wrote, `owner` picks the agent that does it, its model, and its folder, and `isAsync` runs a subagent while Claude goes on. A background task takes `prompt`, `title`, `owner`, and `refuse`, a background fork takes `prompt`, `title`, and `refuse`, and a workflow's agent takes only `refuse` |
198
220
  | `beforeTurnEnd` | `{ note }` or `{ refuse }` | The agent about to stop. `refuse` tells it it may not stop yet, and it reads the reason as an error. `note` gives it new information, which it reads at once and keeps working. On `task.owner`, it is the task about to hand back its answer |
199
221
  | `beforeContextEnd` | `{ summary }` or `{ refuse }` | A compaction about to start. `summary` is the text the new context starts with, which Claude reads as one prompt, in place of the summary Claude Code's model would write, so no model runs. `refuse` keeps the context, whatever another mod answered. When several mods answer `summary`, the last one asked stands |
200
222
 
@@ -247,28 +269,32 @@ sessions.current.beforeModelChange({ model: /haiku/ }, () => ({ refuse: 'This pr
247
269
  sessions.current.onCwdChange(({ cwd, previous }) => mod.log(`moved from ${previous} to ${cwd}`))
248
270
  ```
249
271
 
250
- A `before` method on another session asks the mod over cmod from that session. That session waits up to 5 seconds for the answer, then lets the event happen as it was and logs why.
251
-
252
272
  ### Act on a session
253
273
 
254
- Every action works on the current session and on every other running session. cmod carries an action to another session, and that session's cmod does it there.
274
+ Every action works on the current session and on every other running session, on this computer or through a sessions provider. cmod carries an action to another session, and that session's cmod does it there.
255
275
 
256
276
  - `send` adds a message to the session.
257
277
  - A `Note` is text the agent reads on its next call to the model and the person does not see. It starts no turn. It rejects with `<mod>: Claude Code refused the note: <reason>` when a plugin refuses it.
258
278
  - A `Prompt` is the next prompt, so the agent answers it as a turn of its own. Claude Code holds it until the session is idle, so it never cuts into a turn, and frames it as `The <mod> plugin sent a message:`. With `as: Person`, the agent reads it bare, as the person's own words, while every hook still sees that the mod sent it. It rejects with `<mod>: Claude Code dropped the prompt: <reason>` when a plugin drops it.
259
279
  - `as: Session` sends it to another session as a message from this one, which Claude Code shows as one session writing to another.
260
280
  - To a task's owner, `send` delivers the text to that running subagent.
281
+ - To a session that has ended, a `Prompt` resumes it in a new background Claude Code session, as `claude --bg --resume` does, which answers it.
261
282
  - `startTask` starts a task in the session, the way the Agent tool starts one, and resolves the `Task` once it started. It does not wait for the task to finish. `title` is the task's title, `agentType` names the owner's agent type, such as `Explore`, `name` is the address a teammate's `SendMessage` reaches it by, and `model` and `cwd` change what it runs on and where. It rejects with `<mod>: Claude Code refused the task: <reason>` when a hook refuses it.
262
283
  - With `isBackground: true`, the owner is a new background Claude Code session, as `claude --bg` starts, which starts fresh on the prompt and keeps running after this session ends. It takes no `name`, and rejects with `<mod>: a background task takes no name.` for one.
284
+ - `device` names the device the background session runs on, by its name or its cmod device id. A `device` other than this computer starts through the sessions provider that serves it ([Sessions providers](#sessions-providers)): cmod asks the `startTask` of each provider on that device, its `cmod serve` among them, or of every provider when none runs there, in turn, with the whole task, `device` included, and the first that does not answer not-held starts the session there and answers its task. A device's `cmod serve` starts it in the task's `cwd`, or the project of this session when the task names none, and rejects with `<device> has no folder <cwd>, so the task did not start. Give the task a cwd on <device>.` when that device lacks the folder. With no such provider, the call rejects with `<mod>: no mod starts sessions on <device>, so the task did not start. Leave device out to start it on <this computer>.`
263
285
  - `fork` starts a subagent that keeps this session's whole conversation and does what the prompt says.
264
- - `clone` starts a new background session with a copy of this session's conversation, which carries on with the prompt.
286
+ - With `isBackground: true`, the owner is a new background session on a copy of the conversation, which cmod writes as a transcript of its own, and which carries on with the prompt.
287
+ - `at` is the `id` of a message of the conversation. The copy holds the conversation from its first message through that one, and nothing after it, and needs `isBackground: true`, else the call rejects with `<mod>: a fork through a message runs as a background session. Add isBackground: true.`
288
+ - `end` ends the session's Claude Code once its running turn ends, or at once between turns. Its conversation stays, so a `Prompt` sent to it later resumes it.
289
+ - `visit` marks the session's reply read at `at`, an ISO time, or now, which moves its `visitedAt`.
290
+ - `delete` deletes a session that has ended: its transcripts, its goal, its tasks, and its rows in the index. A running session stays, and the call rejects with `session <id> runs, so it stays. End it first.`
265
291
 
266
- Every task shows in this session's goal. `fork` and `clone` take only `prompt` and `title`, because the owner is a copy of this session. A `beforeTaskStart` listener that refuses a background task or a clone rejects the call with `<mod>: a beforeTaskStart listener refused the task: <reason>`.
292
+ Every task shows in this session's goal. `fork` takes only `prompt`, `title`, `isBackground`, and `at`, because the owner is a copy of this session. A `beforeTaskStart` listener that refuses a background task or a background fork rejects the call with `<mod>: a beforeTaskStart listener refused the task: <reason>`.
267
293
 
268
294
  ```ts
269
295
  await sessions.current.startTask({ title: 'Map the auth code', prompt, agentType: 'Explore' })
270
296
  await sessions.current.fork({ title: 'Write the tests', prompt: 'Write tests for what we just built.' })
271
- await sessions.current.clone({ title: 'Try SQL instead', prompt: 'Rebuild this on SQL.' })
297
+ await sessions.current.fork({ title: 'Try SQL instead', prompt: 'Rebuild this on SQL.', isBackground: true })
272
298
  await sessions.current.startTask({ title: 'Upgrade dependencies', prompt, isBackground: true })
273
299
  ```
274
300
 
@@ -276,13 +302,13 @@ await sessions.current.startTask({ title: 'Upgrade dependencies', prompt, isBack
276
302
  | --- | --- | --- |
277
303
  | `startTask` | absent | a subagent of this session, with its `agentType` |
278
304
  | `fork` | absent | a subagent of this session, `forkedFrom` this session |
279
- | `clone` | `true` | a new session whose `id` is the task's, `forkedFrom` this session |
305
+ | `fork` with `isBackground` | `true` | a new session whose `id` is the task's, `forkedFrom` this session |
280
306
  | `startTask` with `isBackground` | `true` | a new session whose `id` is the task's |
281
307
  - `callTool` runs a tool in the session, such as `{ name: 'Bash', input: { command: 'bun test' } }`, and resolves its `ToolResult`.
282
308
  - `usage` answers the session's model, its context window's fill, its rate limits, and its cost so far, as the status line shows them: `{ model, contextWindow: { tokenCount?, size?, percent? }, rateLimits?: { type, percent, resetsAt? }[], cost?: { usd } }`. Each rate limit is one window of the account's plan: its `type`, such as `five_hour`, `seven_day`, or a Claude gateway's `spend_limit`, how much of it is used, 0 to 100, and when it resets, as an ISO 8601 time. For an ended session and a task's owner it answers the model and the tokens of its last reply, from its transcript, with no rate limits.
283
309
  - `surfaces` answers the Claude apps showing the session now: `'terminal'`, `'desktop'`, `'mobile'`, and `'vscode'`. An ended session shows in none.
284
310
 
285
- A task's owner is a `TaskOwner`, a session without `startTask`, `fork`, `clone`, and `callTool`, because Claude Code gives a subagent no way to start a task or run a tool from outside. It takes `send`, `usage`, and `surfaces`. An action on a session that has ended rejects with `session <id> has ended`, and `send` with `<mod>: session <id> has ended, so it takes no messages.`
311
+ A task's owner is a `TaskOwner`, a session without `startTask`, `fork`, `callTool`, and `delete`, because Claude Code gives a subagent no way to start a task or run a tool from outside. It takes `send`, `usage`, and `surfaces`, and a background task's owner also `end` and `visit`. An action other than `send`, `usage`, `surfaces`, `visit`, and `delete` on a session that has ended rejects with `session <id> has ended, so it takes no <action>. Message it to resume it.`
286
312
 
287
313
  A prompt that comes on a clock, such as every 10 minutes, belongs in a `schedule` job instead, which Claude Code shows as one `Running scheduled task` row ([jobs.md](jobs.md#schedule)).
288
314
 
@@ -328,7 +354,7 @@ type MessageOf<Type> = {
328
354
  }
329
355
  ```
330
356
 
331
- - `id` names the transcript row the message is in. When one row holds several messages, such as a reply with its tool calls, each `id` ends in `:<n>`. `sessions.current.scrollTo(message)` scrolls to the message's row ([ui.md](ui.md#the-conversations-rows)).
357
+ - `id` names the transcript row the message is in. When one row holds several messages, such as a reply with its tool calls, each `id` ends in `:<n>`. `sessions.current.scrollTo(message)` scrolls to the message's row ([ui.md](ui.md#the-conversation-s-rows)).
332
358
  - `at` is its ISO time. `session` is the session's id, and `context` the number of the context it is in, from 1.
333
359
  - `turn` is the `id` of the prompt that opened its turn. A task's message names its owner's turn. A message before the first prompt has none.
334
360
  - `task` is the id of the task whose message it is. A message of the session itself has none.
@@ -440,8 +466,8 @@ sessions.current.onTurnEnd((turn) => {
440
466
  - A context's `number` counts from 1. Its `closedAt` is set at the compaction that ended it.
441
467
  - A goal's text starts as the session's first prompt, and `text()` reads it. `update` replaces it, and `onGoalChange` hears the goal, whose `text()` reads the new text.
442
468
  - A task's `owner` is a `TaskOwner`: the session doing the work, which can't start tasks or call tools. Its goal is the task's prompt, its `agentType` and `model` are what the task runs, and its turns and messages are the task's own. Claude Code's checklist tools, `TaskCreate`, `TaskUpdate`, and `TaskList`, are not tasks: they are its todo list, which `onTodoCreate` and `onTodoComplete` hear.
443
- - A task's `openedBy` is the `callId` of the Agent call that started it, and empty for a task a mod started. Its `closedAt` is set once it answered: a subagent's when it stops, and a background session's at that session's first stop, which its cmod tells this session.
444
- - A task with `isBackground` is done by a separate session, whose `id` is the task's. Its owner reads that session's own turns and messages, and `task.messages()`, `task.tasks()`, and `task.search` read them too. `task.on…` and `task.before…` hear the task's start and end, and `task.owner.on…` hears what happens in that session. Claude Code's own fork agent reads as a fork: an Agent call that forks shows an owner `forkedFrom` this session, with no `agentType`.
469
+ - A task's `openedBy` is the `callId` of the Agent call that started it, and empty for a task a mod started. Its `closedAt` is set once it answered: a subagent's when it stops, a background session's at that session's first stop, which its cmod tells this session, and a background session on another device once its sessions provider tells, with a `Change` of `part: 'running'`, that the session no longer runs.
470
+ - A task with `isBackground` is done by a separate session, whose `id` is the task's. Its owner reads that session's own turns and messages, and `task.messages()`, `task.tasks()`, and `task.search` read them too. `task.on…` and `task.before…` hear the task's start and end. `task.owner` takes no listener, because it is another session. Claude Code's own fork agent reads as a fork: an Agent call that forks shows an owner `forkedFrom` this session, with no `agentType`.
445
471
  - A task of `sessions.current` that Claude Code still runs holds `status`, where its agent stands now, `name`, the name a teammate is messaged by, and `spawnedBy`, the plugin that started it, absent when Claude or the person did. A task read from another session or after its agent ended has none of these. A background task holds the `status` `claude agents` lists for its session.
446
472
 
447
473
  ## Test it
package/docs/testing.md CHANGED
@@ -19,7 +19,7 @@ test('/todo adds the item to the project list', async () => {
19
19
  })
20
20
  ```
21
21
 
22
- Assert what a person or Claude sees: `tested.shown.page.lines()`, `tested.lines(...)`, `tested.shown`, `tested.state`, and what `tested.raise...`, `tested.type(...)`, and `tested.callTool(...)` return.
22
+ Assert what a person or Claude sees: `tested.shown.view.lines()`, `tested.lines(...)`, `tested.shown`, `tested.state`, and what `tested.raise...`, `tested.type(...)`, and `tested.callTool(...)` return.
23
23
 
24
24
  ## TestOptions
25
25
 
@@ -35,6 +35,15 @@ type TestOptions<State, Options> = {
35
35
  readonly links?: Readonly<Record<string, string>>
36
36
  readonly secrets?: Readonly<Record<string, string>>
37
37
  readonly sessions?: readonly FakeSession[]
38
+ readonly providers?: readonly FakeProvider[]
39
+ }
40
+
41
+ type FakeProvider = {
42
+ readonly name: InterfaceName
43
+ readonly mod: string
44
+ readonly program?: string
45
+ readonly device?: { readonly id: string; readonly name: string }
46
+ readonly methods: InterfaceMethods<name>
38
47
  }
39
48
  ```
40
49
 
@@ -50,15 +59,16 @@ type TestOptions<State, Options> = {
50
59
  | `links` | Symbolic links in the fake file system, absolute link path to its target | no links |
51
60
  | `secrets` | The mod's secrets `mod.secrets` starts with, key to value. The fake keeps what the mod stores in memory, and `tested.calls` never holds a value | no secrets |
52
61
  | `sessions` | The earlier conversations `sessions.list` and `sessions.search` read, each `{ id, messages?, goal?, … }` ([Build a conversation](#build-a-conversation)) | none: only the current session, `test-session` |
62
+ | `providers` | The providers of interfaces other mods and programs run, which `providers(name)` and a `program` job's `provider(name)` find ([providers](#providers)) | none: only the tested mod's own `provide` |
53
63
 
54
64
  A project plugin finds its repository through a `.git` entry at `projectRoot`, so a test of one puts it in `files`, such as `files: { '/test/project/.git': '' }`. Without it the project plugin has no repository, and its rules match calls in every folder, as a user mod's do.
55
65
 
56
- The fake session's id is `test-session`, its model is `test-model`, and its home is `/test/home`. So `mod.dataFolder` is `/test/home/.local/share/cmod/data/<name>`, and `mod.folder` is `/test/plugins/<name>`. The mod is installed and draws on a terminal 80 columns wide.
66
+ The fake session's id is `test-session`, its model is `test-model`, and its home is `/test/home`. So `mod.dataFolder` is `/test/home/.local/share/cmod/data/<name>`, `mod.cacheFolder` is `/test/home/.cache/cmod/<name>`, and `mod.folder` is `/test/plugins/<name>`. The mod is installed and draws on a terminal 80 columns wide.
57
67
 
58
68
  ## TestedMod
59
69
 
60
70
  ```ts
61
- type TestedMod<State> = {
71
+ type TestedMod<State, Given> = {
62
72
  start(): Promise<void>
63
73
  readonly raise: Raise
64
74
  fire(event, input, below?): Promise<EventResult>
@@ -80,12 +90,13 @@ type TestedMod<State> = {
80
90
  readonly calls: readonly TestCall[]
81
91
  readonly fakes: Fakes
82
92
  readonly shown: Shown
93
+ readonly providers: { [Index in keyof Given]: Provided<Given[Index]['name']> }
83
94
  }
84
95
  ```
85
96
 
86
97
  ### start
87
98
 
88
- Starts the mod: loads its state, runs `setup`, and adds its listeners, pages, panes, widgets, and jobs. `raise`, `fire`, `lines`, `type`, `hotkey`, `callTool`, `press`, `moveTo`, and `api` start the mod first, so a test calls `start` only before it reads `state` or `shown` with nothing else to run. It rejects with the reason when the mod does not start.
99
+ Starts the mod: loads its state, runs `setup`, and adds its listeners, views, pages, panes, widgets, and jobs. `raise`, `fire`, `lines`, `type`, `hotkey`, `callTool`, `press`, `moveTo`, and `api` start the mod first, so a test calls `start` only before it reads `state` or `shown` with nothing else to run. It rejects with the reason when the mod does not start.
89
100
 
90
101
  ### raise
91
102
 
@@ -124,10 +135,11 @@ fire(event: 'tool.check' | 'prompt.context' | 'cmod.call' | 'ui.render' | 'ui.sc
124
135
  - `fire('cmod.call', { to, method, input })` calls a method `setup` returned, as another mod's `mod.dependencies` does, and returns `{ value }` with what the method returned, or `{ deny }` with the reason it failed. `tested.api.<method>(input)` does the same and resolves the value, or rejects with the reason.
125
136
  - `ui.render`, `ui.scroll`, and `ui.close` are what `lines` and a pane's own events raise. `shown.pane.lines()` and `lines` are the way to draw.
126
137
  - `fire('ui.close', { id, origin: { kind: 'person' } })` closes the pane as Escape or its ✕ does, and draws it once more before it closes, as Claude Code does. A pane's `ui.render` shows a pane Claude Code held back, as Claude Code does when the terminal widens.
138
+ - `claudeCodeOf(tested).fire('ConfigChange', { source: 'user_settings', file_path })` edits a settings file. It runs the `settings.beforeChange` listeners, and then, unless one of them or `below` refused, the `settings.onChange` listeners, as Claude Code loads the change.
127
139
  - `claudeCodeOf(tested).fire('turn.complete', { …, isAborted: true })`, and a Stop fired with an aborted signal, are the person's Esc. From then on `claude.command.run` refuses with `the session was stopped and nobody has sent input since` until the person's next prompt, command, or press, as Claude Code holds every plugin's input after an Esc.
128
140
 
129
141
  ```ts
130
- expect(await tested.fire('cmod.call', { to: 'safe-delete', method: 'restore', input: { path: 'a.ts' } })).toEqual({ value: { restored: true } })
142
+ expect(await tested.fire('cmod.call', { to: 'my-mod', method: 'restore', input: { path: 'a.ts' } })).toEqual({ value: { restored: true } })
131
143
  ```
132
144
 
133
145
  ```ts
@@ -184,11 +196,11 @@ The current session, `test-session`, starts with one message: the cmod plugin's
184
196
  - `endTurn({ answer?, failure? })` ends the open turn, as Claude stopping does. `answer` is Claude's last words, and `failure` ends it on an error, such as `{ type: 'rate_limit', text: 'Rate limited' }`. `{ task }` ends the open turn of that task's owner instead, and the first one a background task's session ends also ends the task.
185
197
  - `compact()` records a compaction boundary, which ends the context and starts the next, then the cmod plugin's device note, as a compaction does.
186
198
  - `startSession({ source, id? })` starts a session as `/clear`, `/resume`, or a fork does, under `id` when it names one, and adds the cmod plugin's device note. A first `startup` is the session the test already started, so it adds no second note. The mod's `session` values are that session's, so a `resume` to `test-session` brings the first session's values back, and `onSessionStart` hears the `source`. Set `fakes.device.read` before a `resume` to test a session that moved to another computer.
187
- - `startTask({ title, agentType?, model?, openedBy?, parent?, isBackground?, forkedFrom? })` starts a task and returns its id, under the task `parent` names when it has one. Its owner runs `agentType` on `model`, and a `Prompt` added with its `task` opens the owner's own turn. `forkedFrom: 'test-session'` starts a fork, `isBackground: true` a background task whose owner is a session of its own, and both together a clone. A message added with a background task's `task` goes into its owner's session.
199
+ - `startTask({ title, agentType?, model?, openedBy?, parent?, isBackground?, forkedFrom? })` starts a task and returns its id, under the task `parent` names when it has one. Its owner runs `agentType` on `model`, and a `Prompt` added with its `task` opens the owner's own turn. `forkedFrom: 'test-session'` starts a fork, `isBackground: true` a background task whose owner is a session of its own, and both together a background fork. A message added with a background task's `task` goes into its owner's session.
188
200
 
189
201
  Each one runs the mod's session listeners, as a new message does in Claude Code. A `MessageDraft` is a message without its place: `{ type: Prompt, text, from? }`, `{ type: Note, text, from? }`, `{ type: Reply, text }`, `{ type: ToolCall, name, input?, callId? }`, `{ type: ToolResult, callId, output, isError? }`, `{ type: Skill, name, text }`, `{ type: InstructionFile, path, text }`, `{ type: AttachedFile, path, attachmentType, text }`, or `{ type: Notice, text }`.
190
202
 
191
- The `sessions` option gives the earlier conversations, each a `FakeSession`: an `id`, its `messages` as drafts, its `goal`, and any `Session` field, such as `title`, `color`, `project`, or `isRunning`. A session that names no `project` is in the test's project. `{ id: 'test-session', color: 'cyan' }` gives the current session its `/color` from the mod's start, and `tested.type('/color cyan')` and `tested.type('/color default')` run Claude Code's `/color`, which changes `sessions.current.color`. `tested.type('/color')` throws, because Claude Code picks a random color for it.
203
+ The `sessions` option gives the earlier conversations, each a `FakeSession`: an `id`, its `messages` as drafts, its `goal`, and any `Session` field, such as `title`, `color`, `project`, or `isRunning`. A session that names no `project` is in the test's project, and one that names no `device` runs on `test-computer`, such as `{ id: 'remote', device: { id: '9c41d07e2b5a4f86a3e1c8d2f07b6e95', name: 'build-box' } }` for a session on another device. `{ id: 'test-session', color: 'cyan' }` gives the current session its `/color` from the mod's start, and `tested.type('/color cyan')` and `tested.type('/color default')` run Claude Code's `/color`, which changes `sessions.current.color`. `tested.type('/color')` throws, because Claude Code picks a random color for it.
192
204
 
193
205
  ```ts
194
206
  import { expect, test } from 'bun:test'
@@ -206,16 +218,16 @@ test('/recall finds the earlier session about the deploy', async () => {
206
218
  })
207
219
  ```
208
220
 
209
- `shown.sent` lists what the mod sent to other sessions and to tasks' owners, each with its `session`, and its `task` for an owner. `fakes.agent.spawn` answers the current session's `startTask` and `fork`, such as `async () => ({ agentId: 'agent-7', model: 'test-model' })`, and `fakes.tool.call` its `callTool`. A background `startTask` and a `clone` start their task in the fake session, after asking the mod's `beforeTaskStart` listeners, and so do another session's `startTask` and `fork`.
221
+ `shown.sent` lists what the mod sent to other sessions and to tasks' owners, each with its `session`, and its `task` for an owner. `fakes.agent.spawn` answers the current session's `startTask` and `fork`, such as `async () => ({ agentId: 'agent-7', model: 'test-model' })`, and `fakes.tool.call` its `callTool`. A background `startTask` and a background `fork` start their task in the fake session, after asking the mod's `beforeTaskStart` listeners, and so do another session's `startTask` and `fork`.
210
222
 
211
223
  A `before` listener on the current session answers what `raise` asks, such as `tested.raise.beforePrompt({ text: 'Deploy to prod' })` or `tested.raise.beforeToolCall({ name: 'Bash', input: { command: 'rm -rf build' } })`.
212
224
 
213
225
  ### lines
214
226
 
215
- - `shown.page.lines()` draws the page the mod shows in the Sidebar now, and `shown.pane.lines()` the pane it shows now, and each returns its rows of text. Each rejects when the mod shows none, such as `No pane of my-mod shows. Open one with pane.open(), from a slash command, a hotkey, or setup.` A page gets `params`, `isFocused`, and `columns`, 80 unless `lines({ columns })` gives another. A pane gets `params`, `title`, `isFocused`, true once an `open` or `toggle` with `{ focus: true }` opened it, `bodyColumns`, which is the `columns` that `lines({ columns })` gives, else the pane's own `columns`, else 80, `placement: 'dock'`, `scroll: { offset: 0, bodyRows: 24 }`, and `view: {}`. `shown.page.id` and `shown.pane.id` name what shows, and `params` holds its params.
216
- - A `Markdown` element in the mod's pages, panes, and widgets draws through the mod's own markdown styles, as it does in a session. Other mods' styles are not drawn.
217
- - A key in a page or widget draws as the cmod plugin gives it to Claude Code: `<mod>/<page>:<key>` in a page, `<mod>/<widget>:<key>` in a widget, and `<mod>/markdown:<scope>:<block>:<key>` in what a markdown style draws, where `<scope>` is the drawn key of the `Markdown` element that holds the block, or its place among the `Markdown` elements without a key, and `<block>` is the block's number in that element's markdown, each from `0`. In the mod's own pane, a style's key draws as `<scope>:<block>:<key>`, and in a reply as `<block>:<key>`. `press`, `input`, and `select` take the key the mod wrote, and a key two blocks share reaches the first.
218
- - Claude Code refuses a whole tree in which two `Input`, `Select`, `Markdown`, `Client`, `Raster`, or `Image` elements have one key, anywhere in it, and a refused Sidebar draws blank for the session. So drawing a page, a pane, a widget area, or a style that does it rejects with Claude Code's own text, such as `cmod: returned a tree Pane refuses: Markdown "my-mod/all:item-text:0" is drawn twice; each takes its own key`. Elements of different types, such as a `Box` and a `Markdown`, may share a key. Claude Code also refuses a `Box`, `Text`, or `Button` whose `hover` names no `scope` and has no `Box` with a key around it, the `Box` itself included, so drawing one rejects with `cmod: returned a tree Pane refuses: Button "my-mod/all:archive" hover has no Box with a key around it; the nearest keyed Box is what the pointer hovers, or a scope names its group`.
227
+ - `shown.view.lines()` draws the view the mod shows in the Sidebar now, `shown.page.lines()` the page, and `shown.pane.lines()` the pane, and each returns its rows of text. Each rejects when the mod shows none, such as `No pane of my-mod shows. Open one with pane.open(), from a slash command, a hotkey, or setup.` A view or page gets `params`, `isFocused`, `columns`, 80 unless `lines({ columns })` gives another, and `surface: 'terminal'`. A pane gets `params`, `title`, `isFocused`, true once an `open` or `toggle` with `{ focus: true }` opened it, `bodyColumns`, which is the `columns` that `lines({ columns })` gives, else the pane's own `columns`, else 80, `placement: 'dock'`, `scroll: { offset: 0, bodyRows: 24 }`, and `view: {}`. `shown.view.id`, `shown.page.id`, and `shown.pane.id` name what shows, and `params` holds its params.
228
+ - A `Markdown` element in the mod's views, pages, panes, and widgets draws through the mod's own markdown styles, as it does in a session. Other mods' styles are not drawn.
229
+ - A key in a view, page, or widget draws as the cmod plugin gives it to Claude Code: `<mod>/<view or page>:<key>` in a view or page, `<mod>/<widget>:<key>` in a widget, and `<mod>/markdown:<scope>:<block>:<key>` in what a markdown style draws, where `<scope>` is the drawn key of the `Markdown` element that holds the block, or its place among the `Markdown` elements without a key, and `<block>` is the block's number in that element's markdown, each from `0`. In the mod's own pane, a style's key draws as `<scope>:<block>:<key>`, and in a reply as `<block>:<key>`. `press`, `input`, and `select` take the key the mod wrote, and a key two blocks share reaches the first.
230
+ - Claude Code refuses a whole tree in which two `Input`, `Select`, `Markdown`, `Client`, `Raster`, or `Image` elements have one key, anywhere in it, and a refused Sidebar draws blank for the session. So drawing a view, a page, a pane, a widget area, or a style that does it rejects with Claude Code's own text, such as `cmod: returned a tree Pane refuses: Markdown "my-mod/all:item-text:0" is drawn twice; each takes its own key`. Elements of different types, such as a `Box` and a `Markdown`, may share a key. Claude Code also refuses a `Box`, `Text`, or `Button` whose `hover` names no `scope` and has no `Box` with a key around it, the `Box` itself included, so drawing one rejects with `cmod: returned a tree Pane refuses: Button "my-mod/all:archive" hover has no Box with a key around it; the nearest keyed Box is what the pointer hovers, or a scope names its group`.
219
231
  - A `Button` draws as Claude Code draws it: `[ Save ]`, or `1: Save` when it is `plain` with a `hotkey`, or `Save` when it is `plain` alone. Borders and colors are not drawn.
220
232
  - `lines(area)` draws the mod's widgets in that area, in the rows the person would see, and returns them. A new widget is in the first row of its first area. A layout given in `files` as `/test/home/.local/share/cmod/widgets.json` places the widgets another way. `{ columns }` sets the row's width, 80 by default. `lines(TurnEnd)` draws under the last turn that ended, and rejects when none has: end one with `tested.endTurn()` first.
221
233
  - `lines(kind, props, requestId?)` draws the mod's style of that kind with `props` and returns its rows. `Default` draws a plain listing of the props it gets, so a test sees what the style changed. Draw `Reply` with a reply's `text` to test a markdown style. Draws that pass one `requestId` grow one streamed reply.
@@ -240,14 +252,14 @@ test('the prompt count shows below the prompt', async () => {
240
252
 
241
253
  ### hotkey
242
254
 
243
- `hotkey(name)` presses the hotkey `defineMod` declared under `name`, as the person pressing its combo does, and awaits its listeners. A page it opens shows as `shown.page`. It rejects a name the mod does not declare.
255
+ `hotkey(name)` presses the hotkey `defineMod` declared under `name`, as the person pressing its combo does, and awaits its listeners. A view it opens shows as `shown.view`, and a page as `shown.page`. It rejects a name the mod does not declare.
244
256
 
245
257
  ```ts
246
258
  test('the hotkey shows the prompt count in the Sidebar', async () => {
247
259
  const tested = testMod(myMod)
248
260
  await tested.add({ type: Prompt, text: 'hello' })
249
261
  await tested.hotkey('toggle')
250
- expect(await tested.shown.page.lines()).toEqual(['Prompts this session: 1'])
262
+ expect(await tested.shown.view.lines()).toEqual(['Prompts this session: 1'])
251
263
  })
252
264
  ```
253
265
 
@@ -257,7 +269,7 @@ test('the hotkey shows the prompt count in the Sidebar', async () => {
257
269
 
258
270
  ### press, input, and select
259
271
 
260
- `shown.page` and `shown.pane` act on an element of what shows as the person does, and await its handler. Each rejects when it does not draw the element: `The pane "<pane>" of <mod> draws no <Element> with the key "<key>".`, or the same for a page.
272
+ `shown.view`, `shown.page`, and `shown.pane` act on an element of what shows as the person does, and await its handler. Each rejects when it does not draw the element: `The pane "<pane>" of <mod> draws no <Element> with the key "<key>".`, or the same for a view or page.
261
273
 
262
274
  - `press(key)` presses the `Button` whose `key` or `label` is `key`, and runs its `onPress`. A `Button` with `href` opens its page as a press in the terminal does ([ui.md](ui.md#a-button-opens-a-web-page-with-href)): it runs `uname -s` and the opener through `fakes.process.run`, or copies the link through `ui.copy` on a Linux with no display.
263
275
  - `press(key, link)` presses the link `link` in the `Markdown` whose `key` is `key`, and runs its `onLinkPress`.
@@ -266,16 +278,16 @@ test('the hotkey shows the prompt count in the Sidebar', async () => {
266
278
  - `tested.press(area, key)` presses a `Button` one of the mod's widgets draws in that area, the way the person presses it in a row.
267
279
 
268
280
  ```ts
269
- await tested.shown.page.input('search', 'deploy')
270
- await tested.shown.page.select('agent', 'explorer')
271
- expect(await tested.shown.page.lines()).toContain('deploy by explorer')
281
+ await tested.shown.view.input('search', 'deploy')
282
+ await tested.shown.view.select('agent', 'explorer')
283
+ expect(await tested.shown.view.lines()).toContain('deploy by explorer')
272
284
  ```
273
285
 
274
- `shown.page.focusedKey()` and `shown.pane.focusedKey()` return the `key` of the `Button`, `Input`, or `Select` that holds the keyboard in what shows, or `undefined` while it does not hold the keyboard or no element does. The page or pane takes the keyboard when it opens with `{ focus: true }`, and its first `autoFocus` element takes it then. `focus(key)` moves it to another element until the page or pane closes or opens again.
286
+ `shown.view.focusedKey()`, `shown.page.focusedKey()`, and `shown.pane.focusedKey()` return the `key` of the `Button`, `Input`, or `Select` that holds the keyboard in what shows, or `undefined` while it does not hold the keyboard or no element does. A view, page, or pane takes the keyboard when it opens with `{ focus: true }`, and its first `autoFocus` element takes it then. `focus(key)` moves it to another element until it closes or opens again.
275
287
 
276
288
  ```ts
277
- await tested.shown.page.press('Answer')
278
- expect(await tested.shown.page.focusedKey()).toBe('answer')
289
+ await tested.shown.view.press('Answer')
290
+ expect(await tested.shown.view.focusedKey()).toBe('answer')
279
291
  ```
280
292
 
281
293
  ### moveTo
@@ -309,6 +321,7 @@ type Fakes = {
309
321
  cmod: { call? }
310
322
  tool: { call? }
311
323
  device: { read? }
324
+ serve: { sessions?, search?, read?, unheld?, visit?, remove? }
312
325
  }
313
326
  ```
314
327
 
@@ -320,7 +333,10 @@ Set a fake to answer a call:
320
333
  - A `program` job is the exception: it catches the `process.spawn` error and retries after 1, 2, 4, 8 and 16 seconds on real timers, so its `ready()` waits past `bun test`'s 5-second timeout. Set `fakes.process.spawn` before a test starts a `program` job.
321
334
  - A `program` job with `singleInstance` spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --no-wait -- <command…>`, so `fakes.process.spawn` gets that `argv`, and the first line the fake writes to standard output is the address of the copy this session runs. A fake that writes `held` and ends tests a session another session runs the copy for: `ready()` resolves the address in `/test/home/.local/share/cmod/instances/<mod>/<program>.json`, which the test gives as `{"address":"unix:/tmp/x.sock"}` in `files`.
322
335
  - A `program` job with `isService` spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --service --env <name> -- <command…>`, with one `--env` for each key of its `environment`, every time it starts, and a fake that writes `held` and ends stands for the service: `ready()` resolves the address in the instance file, as above. A file `/test/home/.local/share/cmod/services/<mod>/<program>.json` in `files` stands for a service an earlier version of the mod wrote: when the mod's setup adds no `isService` program of that name, its start spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --remove-service -- <program>`.
336
+ - A `program` job with `processType` puts `--process-type <type>` right after `--service`.
337
+ - `serve` answers what the cmod plugin asks `cmod serve` for the sessions of the computer, which only a test of the cmod plugin itself reaches: `sessions` lists them, `read(session, part, options)` answers a part such as `messages`, and `unheld(session, action, input)` answers an action on a session no running cmod plugin holds.
323
338
  - A `program` job with `isWebApp` puts `--web-app` right after `<mod>` in that `argv`. `ready()` resolves `tailnetUrl` when the instance file gives one, such as `{"address":"127.0.0.1:62021","tailnetUrl":"https://my-computer.my-tailnet.ts.net:62021/"}` in `files`.
339
+ - A `program` job's `provider(name)` waits until the program is `fatal`, and then rejects as `ready()` does, unless the test fakes that program's provider in `providers` ([providers](#providers)). The test kit's cmod answers each of its asks with no provider 2.5 seconds later on `clock.after`, and the job asks again.
324
340
  - `files` already fills the `fs` fakes: a write is read back, and `list` lists the given files. The fake file system resolves a relative path against `/`, not against the session's working folder, so a mod under test reads `${sessions.current.project}/<file>` rather than a relative path. `fs.list()` with no path rejects.
325
341
  - `ui.open` places every pane by default. A fake that answers `{ isPlaced: false }` holds a pane back.
326
342
  - `ui.scroll` answers `{}` by default, as when the window moved. A fake that answers `{ deny }` refuses the scroll.
@@ -333,7 +349,7 @@ Set a fake to answer a call:
333
349
  - `command.list` answers the slash commands the mod added by default, and `command.run` answers `{}`, a command that printed nothing.
334
350
  - `tool.call` answers `CronCreate`, `CronDelete`, and `CronList` by default, keeping the crons a `schedule` makes in `shown.schedules`. A call to any other tool needs a fake.
335
351
  - `files` takes symbolic links too: `testMod(mod, { files, links: { '/work/skills': '/shared/skills' } })` makes `/work/skills` a link to `/shared/skills`, so a test of `fs.find` or `metadata.update` follows it as a real file system does.
336
- - `testMod` opens no install page: an option with no default and no value fails `start()` with its fix, and the `installer` steps are not shown. Test a step's `render` and `isDone` by calling them with the started `mod`.
352
+ - `testMod` opens no install view: an option with no default and no value fails `start()` with its fix, and the `installer` steps are not shown. Test a step's `render` and `isDone` by calling them with the started `mod`.
337
353
  - The mod below runs `git`, and the `process.run` fake answers in its place.
338
354
 
339
355
  ```ts
@@ -369,6 +385,7 @@ type Shown = {
369
385
  readonly tools: string[]
370
386
  readonly schedules: { readonly id: string; readonly cron: string; readonly prompt: string }[]
371
387
  readonly sent: { readonly session: string; readonly type: string; readonly text: string; readonly as?: string }[]
388
+ readonly view: ShownPart
372
389
  readonly page: ShownPart
373
390
  readonly pane: ShownPart
374
391
  }
@@ -380,6 +397,7 @@ type ShownPart = {
380
397
  press(key: string, link?: string): Promise<void>
381
398
  input(key: string, text: string, type?: 'submit' | 'change'): Promise<void>
382
399
  select(key: string, value: string): Promise<void>
400
+ focusedKey(): Promise<string | undefined>
383
401
  }
384
402
 
385
403
  type ShownToast = { readonly type: ToastType; readonly message: string; readonly description?: string }
@@ -387,7 +405,7 @@ type ShownToast = { readonly type: ToastType; readonly message: string; readonly
387
405
 
388
406
  What the person and Claude would see: the toasts shown now, with a text description, the notes the mod added for Claude with `sessions.current.send` or a `prompt` that follows the state, and the `beforePrompt` notes a subagent reads as it starts from a spawn the test fires, the prompts it sent to the current session, the messages it sent to other sessions in `sent`, the log lines, the debug log lines, the open panes, the slash commands and tools the mod added, and the crons its `schedule` keeps now.
389
407
 
390
- The test kit keeps no Log, as a cmod older than 4.3.0 keeps none, so every error cmod catches for the mod shows in `toasts` ([ui.md](ui.md#toasts)).
408
+ The test kit keeps no Log, so every error cmod catches for the mod shows in `toasts` ([ui.md](ui.md#toasts)).
391
409
 
392
410
  ```ts
393
411
  test('the Mode prompt follows the mode', async () => {
@@ -401,7 +419,53 @@ test('the Mode prompt follows the mode', async () => {
401
419
  })
402
420
  ```
403
421
 
404
- `testing.js` exports the types `TestOptions`, `TestedMod`, `Raise`, `Fakes`, `Shown`, and `TestCall`, so a test helper can take them.
422
+ `testing.js` exports the types `TestOptions`, `TestedMod`, `Raise`, `Fakes`, `Shown`, `TestCall`, and `FakeProvider`, so a test helper can take them.
423
+
424
+ ### providers
425
+
426
+ `providers` holds a fake for each provider the test lists in `TestOptions.providers`, in the same order, so a mod tests its side of an interface before the other side ships. Each fake answers its `methods` and runs where its fields say:
427
+
428
+ - With a `program`, it is that program of `mod`, connected to the test kit's `cmod serve`.
429
+ - With a `program` and a `device`, it is that program on that device. The test kit reaches the device as `cmod serve` does, through a `cmod/Devices` provider, the `devices` program of `testMod`, which `providers('cmod/Devices')` lists too.
430
+ - With no `program`, it is the session code of `mod`, another mod in this session. It takes no `device`, and `mod` is never the tested mod.
431
+
432
+ `providers(name)` lists each fake with its `device`, this computer's unless it names one. A `program` job's `provider(name)` resolves with the fake of its own program, and a fake of the tested mod's program stands for that program, so the job spawns nothing the test fakes.
433
+
434
+ Every call and listener goes through the routing a session uses: the cmod plugin's, then `cmod serve`'s, then the other device's. So a call past its 30 s deadline, a method the fake lacks, and a fake that left fail as they would in a session.
435
+
436
+ Each fake is a `Provided` ([dependencies.md](dependencies.md#interfaces-several-mods-answer-one-name)):
437
+
438
+ - `tell(event, payload)` tells each `onE` listener on the fake.
439
+ - `ask(event, payload)` asks each `beforeE` listener and resolves with their answers.
440
+ - `remove()` makes the fake leave, so `providers(name)` no longer lists it, and a call to it fails with `<mod>'s <program> program does not run, so it answers no <interface>.`
441
+
442
+ Each waits until the calls and listeners the mod already sent reach the fake, so a listener the mod just added hears the next `tell`.
443
+
444
+ The mod below keeps each machine change a `demo/Platform` provider tells, and the test fakes that provider as the `platform` program of the `demo` mod on another device:
445
+
446
+ ```ts
447
+ const machines = defineMod({
448
+ name: 'machines',
449
+ state: { session: { changed: [] as readonly string[] } },
450
+ async setup(mod) {
451
+ for (const platform of await providers('demo/Platform')) {
452
+ platform.onMachineChange((machine) => void (mod.state.session.changed = [...mod.state.session.changed, `${platform.device.name}: ${machine.name}`]))
453
+ }
454
+ },
455
+ })
456
+
457
+ test('a machine change on devbox is kept with its device', async () => {
458
+ const tested = testMod(machines, {
459
+ providers: [{ name: 'demo/Platform', mod: 'demo', program: 'platform', device: { id: 'devbox-id', name: 'devbox' }, methods: { machines: (region) => [`${region}-1`] } }],
460
+ })
461
+ await tested.start()
462
+
463
+ tested.providers[0].tell('MachineChange', { name: 'eu-1' })
464
+ await tested.settle()
465
+
466
+ expect(tested.state.session.changed).toEqual(['devbox: eu-1'])
467
+ })
468
+ ```
405
469
 
406
470
  ## Run the tests
407
471