samagotchi 0.103.0 → 0.104.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -1
  3. data/bin/chi +39 -2
  4. data/docs/architecture.md +36 -24
  5. data/docs/cli.md +86 -12
  6. data/docs/guardrails.md +2 -1
  7. data/docs/internals/context-telemetry.md +6 -2
  8. data/docs/internals/engine.md +4 -1
  9. data/docs/internals/participants.md +137 -0
  10. data/docs/internals/remote-sessions.md +153 -27
  11. data/docs/plugins.md +7 -1
  12. data/docs/sessions.md +4 -2
  13. data/docs/sub-agent.md +8 -0
  14. data/docs/testing.md +68 -4
  15. data/lib/samagotchi/bridge.rb +7 -3
  16. data/lib/samagotchi/bridge_client/event_stream.rb +9 -4
  17. data/lib/samagotchi/bridge_client.rb +4 -1
  18. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  19. data/lib/samagotchi/chi_invocation.rb +13 -16
  20. data/lib/samagotchi/context_status.rb +15 -2
  21. data/lib/samagotchi/engine.rb +16 -6
  22. data/lib/samagotchi/guardrail_wiring.rb +7 -1
  23. data/lib/samagotchi/idle_recap.rb +1 -1
  24. data/lib/samagotchi/idle_scheduler.rb +34 -9
  25. data/lib/samagotchi/kernel_loop.rb +8 -1
  26. data/lib/samagotchi/launch_mode.rb +4 -0
  27. data/lib/samagotchi/llm/chat_loop.rb +8 -1
  28. data/lib/samagotchi/owner_lock.rb +65 -5
  29. data/lib/samagotchi/participants.rb +122 -0
  30. data/lib/samagotchi/remote_attach_command.rb +119 -0
  31. data/lib/samagotchi/run_options.rb +14 -2
  32. data/lib/samagotchi/send_command.rb +1 -1
  33. data/lib/samagotchi/session.rb +8 -4
  34. data/lib/samagotchi/session_access/contract.rb +42 -4
  35. data/lib/samagotchi/session_access/dispatch.rb +29 -10
  36. data/lib/samagotchi/session_access/local.rb +52 -1
  37. data/lib/samagotchi/session_access/remote.rb +112 -16
  38. data/lib/samagotchi/session_access/web_bridge_client.rb +56 -1
  39. data/lib/samagotchi/session_access.rb +85 -3
  40. data/lib/samagotchi/session_continue.rb +1 -1
  41. data/lib/samagotchi/session_inbox.rb +18 -9
  42. data/lib/samagotchi/session_manager.rb +21 -5
  43. data/lib/samagotchi/sessions_command.rb +4 -4
  44. data/lib/samagotchi/share_command.rb +174 -0
  45. data/lib/samagotchi/terminal_ui/attach_launcher.rb +17 -4
  46. data/lib/samagotchi/terminal_ui/attach_link.rb +10 -0
  47. data/lib/samagotchi/terminal_ui/attached_loop.rb +83 -20
  48. data/lib/samagotchi/terminal_ui/formatting.rb +13 -1
  49. data/lib/samagotchi/terminal_ui/local_link.rb +5 -0
  50. data/lib/samagotchi/terminal_ui/remote_connector.rb +66 -0
  51. data/lib/samagotchi/terminal_ui/remote_link.rb +245 -0
  52. data/lib/samagotchi/terminal_ui/version_lines.rb +23 -0
  53. data/lib/samagotchi/terminal_ui.rb +4 -1
  54. data/lib/samagotchi/testing/waiting.rb +92 -9
  55. data/lib/samagotchi/tools/task_runtime.rb +17 -3
  56. data/lib/samagotchi/version.rb +1 -1
  57. data/lib/samagotchi/web/app.rb +187 -74
  58. data/lib/samagotchi/web/grants.rb +302 -0
  59. data/lib/samagotchi/web/participant_access.rb +286 -0
  60. data/lib/samagotchi/web/principal.rb +29 -0
  61. data/lib/samagotchi/web/server.rb +3 -1
  62. data/lib/samagotchi/web/session_access_routes.rb +120 -4
  63. data/lib/samagotchi/worker.rb +26 -5
  64. metadata +10 -1
@@ -1,7 +1,8 @@
1
1
  # Remote sessions
2
2
 
3
3
  `chi --remote NAME <command>` (or `CHI_REMOTE=NAME`) runs chi's session commands (`send`, `answer`, `note`, a
4
- session's `context`, `sessions list/stop/restart/archive/unarchive/delete`) against another machine's sessions. The
4
+ session's `context`, `sessions list/stop/restart/archive/unarchive/delete`) against another machine's sessions, and
5
+ `chi --remote NAME --attach ID` (`--resume ID`, `--dir DIR`, `-p TEXT`) runs the attached TUI on one of them. The
5
6
  sessions, workers, memories and code stay on that machine; this one only sends requests and prints the answers.
6
7
  User-facing behaviour is in [cli.md](../cli.md#remote-sessions). This page is the code's shape, for changing it.
7
8
 
@@ -44,8 +45,14 @@ subcommand:
44
45
  - `here?`: `chi remote …` (`ALWAYS_HERE`), and a bare `--version` / `-h` / `--help`, run locally as usual.
45
46
  - `#run`: an unknown command exits 1. `#refusal` is the single local-only message, `chi <words> runs on this machine
46
47
  only: run it on <remote>` (exit 2). `<words>` comes from `Contract`: a `MACHINE` command, an unlisted command, a
47
- `:local_only` command, subcommand or long flag, or a run flag (`RUN_FLAGS`: `--attach`, `--resume`, `-p`,
48
- `--shared`, `--no-shared`; with no command at all it reads "(a session in this terminal)").
48
+ `:local_only` command, subcommand or long flag, or a run flag `Contract::RUN` lists local-only (`chi --verbose`;
49
+ `--non-interactive` adds "(chi --remote NAME send --new --dir D -m TEXT --wait runs one turn there)"). A flag's
50
+ value (`-p TEXT`, `--model NAME`: `RunOptions.value_switches`) is never read as a flag.
51
+ - **Run flags** (`#run_flags?`: no subcommand, the first word a flag or nothing) don't go to `#run`: bin/chi refuses
52
+ the local-only ones, parses the rest with `RunOptions.parse` (the one run-flag parser) and, before any of its own
53
+ checks (`Session.resolve_id`, the scratch check, `--memory`, `--append-system-prompt`, `Toolkit.from_flags`, …: all
54
+ this machine's), runs `RemoteAttachCommand.new(options, access: Remote.new(entry)).run`. A word left over exits 1.
55
+ Without a remote, `--dir` exits 2 ("--dir goes with --remote; cd there instead").
49
56
  - Otherwise it calls `Remotes.find`, then `Samagotchi.const_get(klass).new(argv, access: Remote.new(entry)).run`.
50
57
  `Remotes::Invalid` (including `NotFound`) exits 2. A `RemoteFailed` that escapes the command prints
51
58
  `chi <cmd>: <message>` and exits 1.
@@ -59,6 +66,14 @@ reads bin/chi's `SUBCOMMANDS` and each command's `FLAGS`. It fails when a subcom
59
66
  command, or a flag of a supported command is missing from both lists, or when the table lists something that doesn't
60
67
  exist. **A new session command or flag fails that spec until you choose one.**
61
68
 
69
+ `Contract::RUN` does the same for bin/chi's run flags (`RunOptions.switches`): `SUPPORTED` (`--attach`, `--resume`,
70
+ `-p`, `--shared`, `--model`, `--thinking`, `--llm-context*`, `--toolkit`, `--tool`, `--no-tool`, `--set`,
71
+ `--parent-answers`, `--no-interrupt`, `--no-default-input`, `--dir`, `--help`, `--version`) or `LOCAL_ONLY`
72
+ (`--no-shared`, `--non-interactive` and `--verbose`: the in-process REPL, P4-D4; `--memory`, `--mute`,
73
+ `--append-system-prompt`: this machine's memories and files, P4-D2/D3; `chi web`'s flags; `--profile`). A config
74
+ registry flag (`--recap-base-url` …) is this machine's config: `LOCAL_ONLY` without being listed
75
+ (`Contract.run_status`). The contract spec fails on a run flag in neither.
76
+
62
77
  ## One request, end to end
63
78
 
64
79
  `chi --remote studio send ab12 -m "fix it" --wait`:
@@ -117,9 +132,20 @@ the session commands*. Keep that table in step with `SessionAccessRoutes`.
117
132
  refused" (also: no answer within 5 s, a read timeout, can't find host, host unreachable, TLS, a broken connection).
118
133
  `ParentWait#run` catches `RemoteFailed` so that `--format json` still prints its error object. Dispatch catches it for
119
134
  the other commands.
120
- - **Auth.** `Remote.auth_failure`: a 401 → "the token was refused (… chi remote add NAME URL --token-file FILE)". A 403
135
+ - **Auth.** `Remote.access_error`, keyed by chi web's error code on any route: a 401 `unauthorized` → "the token was
136
+ refused (… chi remote add NAME URL --token-file FILE)"; a participant's token's own conditions (401 `token_revoked`,
137
+ `token_expired`, 403 `not_permitted`, P3b's 429 `too_many_turns`, `cap_reached`) → one class and message each
138
+ ([participants.md](participants.md#errors-through---remote)). A 403
121
139
  `forbidden` (the Host check) → "chi web answers its LAN IP only: use the LAN IP <ip>, not a hostname", with the IP
122
140
  taken from the 403's detail. `chi remote add` uses the same words through `Remote.info_problem`.
141
+ - **A remote attach.** Before the loop runs (resolve, create, wake, the feature check) every failure is
142
+ `AttachLauncher::Error` or the command's own line, exit 1 (`BadRequest`: exit 2): the wake's 409 `owned_by_tui`
143
+ is `OwnedByTUI` (its message is the one `chi --attach` prints here too: `AttachLauncher.connect` now uses it), its
144
+ 409 `scratch` and 504 `worker_timeout` are `Refused` with `LaunchMode::SCRATCH_LEFTOVER` / `BRIDGE_LATE`'s words
145
+ (`Remote#attach_wake`). Once the loop runs, `WebBridgeClient` turns every `RemoteFailed` into `Unreachable`
146
+ (an `IOError`), so the loop's rescues catch it and `AttachedLoop#worker_down` prints its message as is ("could not
147
+ send the prompt (remote studio (http://…): the token was refused …)"); the stream's end is worded by its reason
148
+ (*Remote attach*, below). A refused token is `AuthRefused < RemoteFailed`, so rediscovery can tell it apart.
123
149
  - **Version skew.** The check is by feature, not by version: `/api/info`'s `features` must include `session_commands`
124
150
  (`SessionAccessRoutes::FEATURE`, mirrored as `Remote::FEATURE` so chi web needn't be loaded). If it's missing,
125
151
  `RemoteTooOld` says "remote NAME runs chi X, too old for this command through --remote: update chi there (chi
@@ -149,14 +175,30 @@ with a "Machine-local" comment; the rest branch on `@access.remote?` (`SendComma
149
175
  | `context`'s default session (`SAMAGOTCHI_PARENT_SESSION`, `ContextCommand#inside_session`) | client | no default: ids are required (`added_by` still reads it) |
150
176
  | A context URL's provider (`ContextProviders.resolve`) | client's bundles | server's bundles (`Remote#context_add_url`) |
151
177
  | UI-only slash commands (`SessionCommands.ui_only_entry`), `--image` ingest, note text (`SessionInbox.checked_text`), push text (`ContextSources.parse_output`) | client | client too (the server checks note and push text again) |
178
+ | A remote attach's `--attach`/`--resume` id (`Session.resolve_id`), the leftover scratch check, the "open in a chi REPL" refusal | client (bin/chi, `AttachLauncher.connect`) | server: `Remote#resolve`, then `POST …/wake`'s 409s |
179
+ | A new attached session's `--toolkit`, `--set`, `--thinking`, `--llm-context*`, `--parent-answers`, `--model` | client (bin/chi) | server: `POST /api/sessions` with `idle: true` and `SetupWords`; its warnings printed as `send --new` prints them |
180
+ | The status row's default model, the newest chi for the version line, what a parent may pick (exit 3) | client | server: the wake's `default_model`, `installed`, `parent_strategy` (the last read again from `GET …/worker` at report time) |
181
+ | `@path` images (ingest, convert, downscale) | client, into the session's `images/` | client, into a scratch dir, then uploaded (`POST …/images`) |
182
+ | Prompt history (↑), the `no_interrupt` / `no_default_input` baselines, Tab's `@path` | client | client (this terminal's; `history: false` on every POST); no memory-name completion remotely (P4-D11) |
152
183
 
153
184
  ## Trust model
154
185
 
155
186
  - **The token is the owner.** In LAN mode (`web.host: lan`) every non-loopback request needs the `Web::Token` access
156
- token, as the `chi_token` cookie or `Authorization: Bearer` (`App#token_gate`). Passing the gate grants everything,
157
- because no route checks roles (`local_viewer?` only hides `editor_url`). This runs over plain http, which is fine on
158
- a home LAN, the same as the phone page. A loopback chi web without LAN mode needs no token
187
+ token, as the `chi_token` cookie or `Authorization: Bearer` (`App#token_gate`). This runs over plain http, which is
188
+ fine on a home LAN, the same as the phone page. A loopback chi web without LAN mode needs no token
159
189
  (`Remotes::Entry#token` is nil, and no header is sent).
190
+ - **Every request has a principal.** `App#principal_for` resolves who a request is, as a `Web::Principal`
191
+ (`web/principal.rb`): `Principal::OWNER` for a peer the gate doesn't ask (no LAN mode, or a loopback peer) and for
192
+ the access token; a participant for a live grant's `chp_` token; nil (a 401) for anyone else (`token_revoked` /
193
+ `token_expired` for an ended grant's). `#token_gate` uses it, and `#route` keeps it in the request's env
194
+ (`App#principal(req)`), so a handler reads the principal and never trusts the body for it. Each `ROUTES` row names
195
+ who may use it (its permission column, `:owner` or `:participant`; `Principal#permits?`; any other answers 403
196
+ `not_permitted`). The owner passes every route (`local_viewer?` only hides `editor_url`). A participant, a
197
+ `Web::Grants` token for one session, reaches only the `:participant` rows, and another session's id is a 404
198
+ ([participants.md](participants.md)). A turn's author comes from the principal
199
+ (`Principal#author`, nil for the owner) and rides with the turn (`SessionManager.deliver_turn(author:)` →
200
+ `BridgeClient#post_turn` → `Bridge#handle_post_turn` → the input file, the fallback's too → the turn's `origin`);
201
+ an owner's turn carries none, so its input file and session are byte for byte as before.
160
202
  - **The token at rest.** `remotes.yml` is written through `AtomicFile.write(perm: 0o600)`. `Entry#inspect` / `#to_s`
161
203
  redact the token. `chi remote ls` never prints it (JSON shows `token: true|false`). `HTTPTransport#inspect` and
162
204
  `Remote#inspect` show only the label.
@@ -166,6 +208,12 @@ with a "Machine-local" comment; the rest branch on `@access.remote?` (`SendComma
166
208
  - **`client_id` only tightens.** `handle_question_answer` forwards `client_id` only when it is `ClientId::CLI_ANSWER`
167
209
  and drops any other value, so a remote answer is held to the parent rules and can't pass for the user's own. On
168
210
  create and turn, a `cli:send` / `cli:answer` id adds checks (`CLI_SEND_IDS`) and never removes any.
211
+ - **A remote attach's `client_id`** (`tui:<pid>@<host>`, P4-D6) is forwarded as given on `/turn` and `/command`, as
212
+ the page's is: the token is the owner. Only the cli ids (`cli:send`, `cli:answer`) add checks there; a parent
213
+ agent driving the TUI from a pipe answers as `cli:answer`, held to that machine's `guardrails.parent_answers`.
214
+ `POST …/exit` passes on only a `tui:` id: any other is dropped, so the worker counts every stream as a UI, which
215
+ only keeps it up. `POST …/wake` spawns a worker, which `/turn` already did. `!cmd` typed in a remote TUI runs on that machine, under
216
+ its guardrails.
169
217
  - **Context push is an inbound path.** A remote `context push` writes text that can wake a session. It carries the same
170
218
  trust as a local `chi context push` on that machine, the owner's token ([guardrails.md](../guardrails.md)). A remote
171
219
  `context add --cmd` runs its command on the session's machine.
@@ -197,20 +245,43 @@ with a "Machine-local" comment; the rest branch on `@access.remote?` (`SendComma
197
245
  (`spec/support/web_server.rb`) followed through a `WebBridgeClient` (join, a turn, a restart, a stop). It runs
198
246
  over loopback, so chi web's token gate is skipped there; `spec/web/lan_auth_spec.rb` covers the gate on the
199
247
  stream, wake and worker routes.
200
- - By hand: the smoke-run skill (`.claude/skills/smoke-run`, kept in the checkout, not in git) covers chi web.
201
- It has no remote recipe yet. The real lane is laptop → Studio over the LAN: `chi remote add`, `send --new --wait`,
248
+ - The remote TUI: `spec/terminal_ui/remote_link_spec.rb` (rediscovery reasons, the wake on a reconnect, images, the
249
+ join's reads, `HANDLED_COMMANDS` covering every attached terminal command), `spec/terminal_ui/attached_loop_remote_spec.rb`
250
+ (the loop's `--remote` lines, `/exit`, `/exit --delete`, `/archive`, `/stats`, `/recap`, a join with that machine's
251
+ saved records, `Unreachable` mid-session, the reasons' and reconnect lines),
252
+ `spec/terminal_ui/remote_connector_spec.rb`, `spec/remote_attach_command_spec.rb`,
253
+ `spec/session_access/remote_attach_spec.rb` (wake, worker, feature check through `RackTransport`), and bin/chi's
254
+ branch in `spec/session_access_dispatch_spec.rb`.
255
+ - By hand: the smoke-run skill (`.claude/skills/smoke-run`, kept in the checkout, not in git) covers chi web; its
256
+ cli-tui recipe has remote attach on one machine (two isolated envs, `chi remote add self http://<LAN IP>:PORT`, the
257
+ terminal's commands through chi web). The real lane is laptop → Studio over the LAN: `chi remote add`, `send --new --wait`,
202
258
  an exit-3 `answer`, `context push`, `note`, then `CHI_REMOTE=studio sh scripts/pair <id>` from the laptop's Claude.
203
259
 
204
- ## Remote attach (the plumbing)
260
+ ## Remote attach
261
+
262
+ `chi --remote NAME --attach ID`: the attached TUI (`AttachedLoop`) on a session of another machine. The TUI still
263
+ talks to a worker's Bridge interface; chi web relays each request to that Bridge on loopback.
205
264
 
206
- What a remote attached TUI (`chi --remote NAME --attach ID`, not wired yet) will run on. The TUI still talks to a
207
- worker's Bridge interface; chi web relays each request to that Bridge on loopback.
265
+ ```
266
+ bin/chi ── RunOptions.parse ── a remote, run flags ──> RemoteAttachCommand ── AttachLauncher.run(connector: RemoteConnector)
267
+ │ POST …/wake
268
+ AttachedLoop.new(link: RemoteLink) ── link.client: WebBridgeClient
269
+ │ one-shots and the stream
270
+ chi web (that machine) ── the worker's Bridge
271
+ ```
272
+
273
+ ### The plumbing
208
274
 
209
275
  **chi web's routes** (tabled in [architecture.md](../architecture.md)): `POST …/wake` (`Local#wake`: the live worker
210
276
  as is, else `SessionManager.resume_session` and `BridgeClient.wait_for`, at most `WAKE_MAX_WAIT` 10 s, the attach's
211
- `BRIDGE_WAIT` here; a leftover scratch session is refused in `LaunchMode::SCRATCH_LEFTOVER`'s words, a late Bridge
212
- in `LaunchMode::BRIDGE_LATE`'s) and `GET …/worker` (`Local#worker`: live, the stop marker, the owner; it never starts
213
- one), both answering the worker as `{epoch, started_at, version, features}`; `POST …/turn` takes `no_interrupt`
277
+ `BRIDGE_WAIT` here, after waiting for a worker still leaving to let go of its lock, `OwnerLock.await_release`: its
278
+ recap on leave's timeout plus 5 s; a leftover scratch session is refused in `LaunchMode::SCRATCH_LEFTOVER`'s words, a late Bridge
279
+ in `LaunchMode::BRIDGE_LATE`'s) and `GET …/worker` (`Local#worker`: live, the stop marker, the owner, `leaving` and `leave_seconds` when its worker
280
+ is leaving, `WorkerState#leaving?`; it never starts one; it also names that machine's `default_model`), both answering the worker as `{epoch, started_at, version,
281
+ features}`; `POST …/exit` (the worker's exit, `client_id` passed on only when it is a `tui:` one), `POST …/recap`,
282
+ `GET …/stats` (the stats, else an older worker's state metrics), `GET …/children/counts` (`ChildrenStatus.counts`)
283
+ and `GET …/turns/:turn_id/records` (`SessionMetrics`' saved tool records and turn record), every refusal with
284
+ `{error, detail, message}`; `POST …/turn` takes `no_interrupt`
214
285
  (`SessionManager.deliver_turn(no_interrupt:)`, to the Bridge or into the input file); `POST …/command`'s 501 for a
215
286
  builtin the worker lacks names it (`command`). `/api/info`'s feature `remote_attach`
216
287
  (`SessionAccessRoutes::ATTACH_FEATURE`, mirrored as `Remote::ATTACH_FEATURE`) says a chi web has them.
@@ -221,10 +292,11 @@ builtin the worker lacks names it (`command`). `/api/info`'s feature `remote_att
221
292
 
222
293
  **`SessionAccess::WebBridgeClient`** (`session_access/web_bridge_client.rb`, built by `Remote#attach_client(id,
223
294
  worker:)` from a wake's or worker read's `worker`): `post_turn` (`no_interrupt`, `history: false`), `post_command`,
224
- `answer`, `dismiss_question`, `dequeue`, `cancel`, `follow`, `connect_stream`, `session_id`, `worker_key`, `port`
225
- (nil). One-shots go through `Remote#route_request` (the server check, the auth and missing-route mapping), a new
226
- connection each, with a 45 s read timeout (chi web's 30 s Bridge read plus its wait for a waking Bridge; 75 s for
227
- answer and dismiss, which chi web sends twice on a 408). Every reply goes through **one status mapper**,
295
+ `answer`, `dismiss_question`, `dequeue`, `cancel`, `request_exit`, `request_recap`, `stats`, `children_counts`,
296
+ `turn_records`, `follow`, `connect_stream`, `session_id`, `worker_key`, `port` (nil). One-shots go through
297
+ `Remote#route_request` (the server check, the auth and missing-route mapping), a new connection each, with a 45 s
298
+ read timeout (chi web's 30 s Bridge read plus its wait for a waking Bridge; 75 s for answer and dismiss, which chi web
299
+ sends twice on a 408; 5 s, `JOIN_READ_TIMEOUT`, for the join's reads: the children counts and a turn's records, L9). Every reply goes through **one status mapper**,
228
300
  `WebBridgeClient::Mapping.response(route, reply, …)`:
229
301
 
230
302
  | chi web's answer | What the loop gets |
@@ -253,21 +325,72 @@ passes none):
253
325
  sidecar before it ends the open streams: the reconnect finds no sidecar, chi web waits for the next worker's
254
326
  (`await_bridge_port`) and proxies the cursor to it. A sidecar still naming the dead port would get an empty 200
255
327
  (the proxy's connects refused), and the stream would move by rediscovery (`worker_changed`, then a `snapshot`).
256
- - `announce_retries:` `stream_retrying` once a reconnect fails, `stream_resumed` at the next frame.
328
+ - `announce_retries:` `stream_retrying` once a reconnect fails and rediscovery neither moved the stream nor ended it
329
+ (a stop prints its stopped line alone), `stream_resumed` once a stream answers again (a replay may bring no frame).
257
330
 
258
331
  `EventStream` compares a rediscovered client's key with the key it last followed, not with the client object's, so a
259
332
  rediscovery that updates the same client in place is a move. Rediscovery may give a reason with its answer
260
333
  (`[nil, reason]` while it retries, `[:gone, reason]` to stop, `AttachLink#live_worker`'s shape), and `stream_closed`
261
- carries the last one (`unreachable` without). `LocalLink#live_worker` gives `stopped` for a stopped session, but
262
- `AttachedLoop#live_worker` passes only the client on, so the local closing line is unchanged.
334
+ carries the last one (`unreachable` without). `AttachedLoop#live_worker` passes the link's pair on; on this machine
335
+ the closing line reads `stopped` as `unreachable`, the words it always had.
336
+
337
+ ### The remote TUI
338
+
339
+ - **`RemoteAttachCommand`** (`remote_attach_command.rb`) takes RunOptions' parsed options: `LaunchMode.attached_conflict`
340
+ (exit 1, the same words); `--attach`/`--resume` resolved there (`Remote#resolve`); without them a new session,
341
+ which needs `--dir`, that machine's absolute path (exit 2, before anything is asked): `Remote#create` with
342
+ `NewSession(idle: true)` and the setup flags as typed (`SetupWords`), its warnings printed as `send --new` prints
343
+ them, its `-p` sent once attached. An existing session prints the ignored-flags line (`--dir` among them) and gets
344
+ `--model`/`--llm-context*`/`--thinking` as first commands, as here. `Remote#require_feature!("remote_attach")` comes
345
+ first (without it, `Remote.too_old_message`). Then `AttachLauncher.run(connector: RemoteConnector)` and
346
+ `AttachLauncher.exit_status`.
347
+ - **`RemoteConnector`** (`terminal_ui/remote_connector.rb`): `#connect` is `Remote#attach_worker` (a worker there
348
+ still leaving prints `LaunchMode::RECAP_WAIT`, as a local attach does, and the wake's read timeout grows by its
349
+ `leave_seconds`), then `Remote#attach_wake` (`POST …/wake`, `WakeReply`) and the `RemoteLink`; `#client_id` is `tui:<pid>@<short hostname>` (P4-D6).
350
+ - **`RemoteLink`** (`terminal_ui/remote_link.rb`), the `AttachLink`:
351
+ - `client`: one `WebBridgeClient` for the attach; `switched(event)` keeps it (its key the event's epoch) and reads
352
+ the new worker's features (`GET …/worker`).
353
+ - `live_worker`: `Remote#attach_worker` (`WorkerReply`) with a reason (M5): `stopped` and `unknown_session` (gone),
354
+ `auth_failed` (`AuthRefused`) and `too_old` (`RemoteTooOld`) end at once; `unreachable` (a transport
355
+ `RemoteFailed`) and `worker_gone` (chi web answers, no live worker, not stopped) retry through
356
+ `RECONNECT_DELAYS` (about a minute, P4-D5). A worker gone for `WAKE_AFTER` (3 s, so a restart's new worker isn't
357
+ raced) is woken once per outage with `POST …/wake` (P4-D9). `failure` keeps the last message for the closing line.
358
+ - `queues?`/`unqueues?`: the worker's `features`. `ingest_images`: `ImageStore.ingest` into a scratch dir here, then
359
+ `Remote#upload_images`; `close` (`AttachLauncher.run`'s `ensure`) removes the dir.
360
+ - `installed_version`: the wake's (that machine's). `default_model_name`: the wake's, then the one `GET …/worker`
361
+ names after a worker change (the loop forgets its copy on `worker_changed`). `parent_strategy`: `GET …/worker`'s,
362
+ read at report time (L4), the wake's when that fails.
363
+ - `completions`: `@path` (this machine's files: images are ingested here), no memory names (P4-D11).
364
+ - `via`: `Remote#invocation`, so every printed command reads `chi --remote NAME …` (`ChiInvocation#attach` and
365
+ `#resume` have their remote forms; `shows`/`open_it` name the attach too).
366
+ - The terminal's own commands through chi web: `/exit` is `WebBridgeClient#request_exit` (`POST …/exit`, this
367
+ TUI's `tui:` id); `/exit --delete` adds `Remote#delete(id, force: true)` and `/archive` `Remote#archive` once the
368
+ worker answered (their refusals the classes `SessionManager` raises here, a transport failure `Unreachable`);
369
+ `/stats` is `GET …/stats` and `/recap` `POST …/recap`. A failure the loop shows in its own words through a
370
+ remote: "(no metrics: …)", "(no recap: …)", the exit lines' "could not ask the worker to stop: …". A chi web
371
+ with `remote_attach` but without one of these routes (`serve_static`'s 404) reads as `RemoteTooOld`, so the line
372
+ carries `Remote.too_old_message`'s words.
373
+ - The join's reads swallow every failure (a debug log line): `saved_tool_records` / `saved_turn_record` are one
374
+ `GET …/turns/:turn_id/records` per join (the second call takes what the first read) and give `[]` / nil;
375
+ `children_counts` gives `Counts.none`.
376
+ - `HANDLED_COMMANDS` must list every `local: true` command shown in the attached TUI (`remote_link_spec`), so a new
377
+ terminal command can't silently fall through remotely.
378
+ - **The loop's lines.** `stream_retrying` → "(lost the connection to NAME; reconnecting…)", `stream_resumed` →
379
+ "(reconnected)". `stream_closed`, by reason: "The session was stopped on NAME. Resume it with: chi --remote NAME
380
+ --resume ID"; "Lost the session: <the auth or too-old words>"; "Lost the connection to NAME (connection refused).
381
+ Re-attach with: chi --remote NAME --attach ID"; "The session's worker on NAME is gone. Resume it with: …"; "The
382
+ session is gone from NAME (deleted)." A restart is one "chi> the session's worker restarted on chi X" (the epoch's
383
+ `worker_changed`, then the new Bridge's `reset`). The version line across machines (P4-D10,
384
+ `VersionLines.across_machines`): "chi> the session's worker runs chi W; NAME has I installed: chi --remote NAME
385
+ sessions restart ID" and "chi> this terminal runs chi T (NAME has I): chi update here".
263
386
 
264
387
  ## Extension points (not built yet)
265
388
 
266
- - **Remote attach.** `--attach`, `-p` and `--resume` are refused today (`Dispatch::RUN_FLAGS`). The seams it plugs into
267
- are in place:
389
+ - **Remote attach** is built (*Remote attach*, above); its seams, for another kind of link:
268
390
  - `TerminalUI::AttachLink` (terminal_ui/attach_link.rb): what `AttachedLoop` knows about *where* the session is,
269
391
  beside its client: `live_worker` (`[client, nil]`, `[nil, nil]`, `[:gone, reason]`) and `switched(event)` for
270
- the stream's rediscovery and a restart; `queues?` / `unqueues?`; `stats_metrics`; `ingest_images`; the saved
392
+ the stream's rediscovery and a restart; `queues?` / `unqueues?`; `stats_metrics` (may raise `IOError`: the loop
393
+ shows a remote's message); `ingest_images`; the saved
271
394
  records and `children_counts`; `delete_session`, `archive_session`; `installed_version`, `default_model_name`,
272
395
  `parent_strategy`; `completions` (Tab's `@path` and `#memory`); `reconnect_delays`; and `via`, the
273
396
  `ChiInvocation` every printed command is spelled with. `LocalLink` is this machine's; `AttachedLoop.new` makes
@@ -285,8 +408,7 @@ carries the last one (`unreachable` without). `LocalLink#live_worker` gives `sto
285
408
  branch can take the parsed options before bin/chi's local checks; `LaunchMode.attached_conflict` and
286
409
  `LaunchMode::SCRATCH_LEFTOVER` / `BRIDGE_LATE` are the words both would print.
287
410
 
288
- The plumbing below (*Remote attach*) is built; `RemoteLink`, the connector and bin/chi's branch are not. chi web
289
- still has no route for exit, recap, stats or a turn's saved records.
411
+ An https chi web (a pod behind a proxy) needs TLS and chunked decoding in `StreamRequest`.
290
412
  - **Shared sessions (guests, roles).** The only access point is `App#token_gate`: one token, the owner's. Roles would
291
413
  attach there and to the routes. The claude-peer / codex-peer inbox leg (chi → this machine's Claude or Codex) would
292
414
  need a relay through chi web.
@@ -296,6 +418,10 @@ carries the last one (`unreachable` without). `LocalLink#live_worker` gives `sto
296
418
 
297
419
  ## Known gaps
298
420
 
421
+ - A prompt typed while a remote stream reconnects is usually accepted (chi web's `/turn` resumes the worker, else
422
+ writes an input file) where a local one fails "could not send the prompt". A POST whose reply is lost reads as
423
+ "could not send", yet ran: sending it again runs it twice (no idempotency key yet).
424
+
299
425
  - Through a remote the pair scripts (`bundles/{claude,codex}-peer/scripts/pair`) detect `CHI_REMOTE` or `--remote` in
300
426
  `CHI` the way Dispatch does, and attach only `*-outbox`: chi there can't reach this machine's Claude socket or Codex
301
427
  thread, so chi's replies reach Claude only when relayed.
data/docs/plugins.md CHANGED
@@ -1207,7 +1207,13 @@ bundle.shutdown
1207
1207
  - **More:** `bundle.plugin` (the instance `register` ran on, for its own methods), `bundle.declarations` (the
1208
1208
  `chi.*` calls `register` made, in order, as made), `bundle.namespace`, `bundle.hook_names(event)`,
1209
1209
  `bundle.run_inits`, `sandbox.write_memory(name, text, scope:)` / `memory_dir`, `sandbox.fake_executable(name,
1210
- body)` (first on `PATH`), `wait_until { }`.
1210
+ body)` (first on `PATH`).
1211
+ - **Waiting:** `wait_for(what, timeout: 5, seen: nil) { … }` polls the block until it is truthy and returns that
1212
+ value, or raises `Samagotchi::Testing::Waiting::TimedOut` ("timed out after 5 s waiting for <what>", plus
1213
+ `; last saw: <value>` from the `seen:` callable). `join!(thread, what)` and `pop!(queue, what)` are
1214
+ `Thread#join` and `Queue#pop` with that deadline and message; `wait_until { }` returns the block's last value
1215
+ instead of raising, for a wait whose timeout is fine; `process_gone?(pid)`. Every deadline is multiplied by
1216
+ `SPEC_TIME_SCALE` (default 1, at least 1; chi's CI sets 2), so a slow runner gets headroom.
1211
1217
  - **Model notes:** `sandbox.model_notes_for(model_id, small: nil, cwd: nil)` lists the model notes chi would load
1212
1218
  into the prompt for a bare model id (`deepseek/deepseek-v4.1-flash`, no host prefix), from the sandbox's
1213
1219
  memories (an installed bundle's included), matched as chi matches them: each `models:` glob against the id and
data/docs/sessions.md CHANGED
@@ -17,10 +17,12 @@ Sessions are plain files — no DB. Each session is `~/.local/state/samagotchi/s
17
17
 
18
18
  A session is deleted if **expired by age OR overflow by count** (unless `keep_status` or the live-owner guard: a session a worker or `chi` still has open is never pruned). A session's `status` is its turn state (`idle`/`running`), not whether a worker is alive. Orphan dirs without a `*.json` are never deleted, except skeleton-only ones (see *Empty sessions*). Deletion removes both `*.json` and sidecar dir atomically. Set both `DAYS=0` and `MAX=0` to retain forever.
19
19
 
20
- **Worker idle exit:** a background worker (plain `chi`, Web UI sessions) exits after `SAMAGOTCHI_SESSION_IDLE_EXIT_MINUTES` (`session.idle_exit_minutes`, default `30`, `0`=never) with no turn running or queued, no client on its stream (an open web tab or attached terminal keeps it), no reminder registered, no background task it watches for a [finish wake](internals/background-tasks.md#finish-wake) still running or waiting for its wake turn (with `session.task_wakes: wake`; not one whose turn you stopped) and no continue offer (the step-limit question) waiting for an answer. It removes `bridge.json` and frees `owner.lock`; the next prompt or `--attach` wakes a new worker (`WorkerIdleExit`, `SessionManager.run_session_loop`). A client can also ask the worker to exit right away (Bridge `POST /session/:id/exit`; `/exit` in an attached terminal does, `/detach` doesn't): the same rules apply without the timeout (even with `0`), and the asker's own stream doesn't count. The reply names what keeps it up (for background tasks, which ones and how to stop them). The session's status stays as it was, not `stopped`. With `"restart": true` (a worker whose sidecar lists the `restart` feature) the worker hands the session to a new worker on the newest chi installed: it leaves, then starts its successor once its lock is free, keeping even an empty session and writing no recap. Clients' streams don't hold a restart (a web tab and an attached terminal reconnect to the new worker); what would be lost does: a turn running or queued, a continue offer, a question or approval waiting, reminders, a `/btw` (or another anytime command) still running, a delegate's approval relay open or settled in the last 10 minutes, and background tasks this conversation started that still run (`WorkerIdleExit#hold_for_restart`; 409 `held` with `reason`). What lives only in the worker's memory goes: the event history a late client replays, the kept `/btw` answers, a plugin's in-memory state.
20
+ **Worker idle exit:** a background worker (plain `chi`, Web UI sessions) exits after `SAMAGOTCHI_SESSION_IDLE_EXIT_MINUTES` (`session.idle_exit_minutes`, default `30`, `0`=never) with no turn running or queued, no client on its stream (an open web tab or attached terminal keeps it), no reminder registered, no background task it watches for a [finish wake](internals/background-tasks.md#finish-wake) still running or waiting for its wake turn (with `session.task_wakes: wake`; not one whose turn you stopped) and no continue offer (the step-limit question) waiting for an answer. It removes `bridge.json` and frees `owner.lock`; the next prompt or `--attach` wakes a new worker (`WorkerIdleExit`, `SessionManager.run_session_loop`). As its Bridge stops, the worker marks the lock leaving (`OwnerLock#mark_leaving`: `leaving_at`, and `leave_wait`, its `recap.timeout`), since it keeps the lock while it writes the recap on leave: an `--attach` or `--resume` meanwhile (here, or a remote one through chi web's wake) prints `(waiting for the session to finish its recap…)` (`LaunchMode::RECAP_WAIT`) and waits for the release, up to that budget plus `OwnerLock::LEAVE_MARGIN` (5 s), before starting the new worker (`OwnerLock.await_release`). A lock held by a worker that isn't leaving (a live one, a stuck one) is left as before. A client can also ask the worker to exit right away (Bridge `POST /session/:id/exit`; `/exit` in an attached terminal does, `/detach` doesn't): the same rules apply without the timeout (even with `0`), and the asker's own stream doesn't count. The reply names what keeps it up (for background tasks, which ones and how to stop them). The session's status stays as it was, not `stopped`. With `"restart": true` (a worker whose sidecar lists the `restart` feature) the worker hands the session to a new worker on the newest chi installed: it leaves, then starts its successor once its lock is free, keeping even an empty session and writing no recap. Clients' streams don't hold a restart (a web tab and an attached terminal reconnect to the new worker); what would be lost does: a turn running or queued, a continue offer, a question or approval waiting, reminders, a `/btw` (or another anytime command) still running, a delegate's approval relay open or settled in the last 10 minutes, and background tasks this conversation started that still run (`WorkerIdleExit#hold_for_restart`; 409 `held` with `reason`). What lives only in the worker's memory goes: the event history a late client replays, the kept `/btw` answers, a plugin's in-memory state.
21
21
 
22
22
  **Empty sessions:** a session nothing happened in is deleted when it is left, so `chi` → `/exit` leaves no trace. Empty means: no messages, no turn tried (a failed turn leaves a `last_prompt` and keeps it), no memory used, no pending question, nothing in `input/`, `notes/` or `images/`, nothing in its dir beyond the worker's skeleton, mode `assist`, and the default model. A session on another model (`/model`, `--model`) counts as prepared and stays (`SessionManager.discardable?`). The worker checks as it idle-exits or leaves on a client's request and deletes it once its lock is free, checking again first. The attached `/exit` then says `Detached; the session is empty: it will be discarded if nothing arrives before its worker stops.` (a note or prompt landing first keeps it). The REPL (`--no-shared`) does the same at `/exit` or Ctrl-D. The retention sweep catches those killed first: empty sessions over an hour old with no live owner, whatever `DAYS`/`MAX`, and skeleton-only dirs with no `*.json`. `session.keep_empty: true` (env `SAMAGOTCHI_SESSION_KEEP_EMPTY`) turns all of it off.
23
23
 
24
+ **Participants:** a session its owner invited participants to review (`chi share`, see [CLI: Participants](cli.md#participants)) has two more files in its dir, neither in the session JSON: `participants.json` (0600), who is or was invited (`{participants: [{id, kind, name, role, added_at, expires_at, removed_at}]}`, never a token), which chi web and `chi share` write and the worker reads (`Participants.read`); and `participants.jsonl` (0600, append-only), the audit trail (grants issued, revoked and expired, each new peer, refused routes). A session nobody was invited to has neither. A continue, a fork or a delegate starts with no participants. Archiving or deleting the session revokes its links. See [internals/participants.md](internals/participants.md).
25
+
24
26
  **Attached by default:** plain `chi` and `chi --resume ID` run their session in a background worker and attach to it (`session.shared`, default `true`; env `SAMAGOTCHI_SESSION_SHARED`). The worker runs in the session's `working_directory`, so `!commands` and tools see the directory the session was started in. `session.shared: false` keeps the in-process REPL, `--no-shared` does for one run; a session the REPL has open can't be attached ("close it there first"). See [CLI: Sharing a session](cli.md#sharing-a-session).
25
27
 
26
28
  **Which chi a worker runs:** a new or woken worker runs the newest chi installed for this Ruby (`gem 'samagotchi', '>= 0.a'`, prereleases count), whoever starts it: a `chi web` or terminal still on an older chi starts it on the newest one, so a stale process doesn't spread old code. A worker already running keeps its version until it is restarted. Should the newest install fail to load, the worker runs the starter's own version and logs `boot_fallback` (its stdio is `/dev/null`). Under `bundle exec` the worker drops Bundler's env first, so it activates the newest gem too. Two consequences: a wrapper pin (`chi _0.17.0_ web`) pins only that process, not the workers it starts; and a source checkout (`bin/chi` in a worktree, `bundle exec`) starts its workers on its own `lib/`, but a session it made that is later woken by an installed chi (`chi web`, `chi send`) runs the installed code. The names an older starter hands a newer worker (the boot line, its env keys, `input/` files, the session JSON fields and the Bridge routes sessions use on each other) are pinned in `spec/worker_contract_spec.rb`.
@@ -29,7 +31,7 @@ A session is deleted if **expired by age OR overflow by count** (unless `keep_st
29
31
 
30
32
  **A worker killed mid-turn:** while a turn runs, its worker keeps it on disk (`TurnCheckpoint`): the session file has the turn so far (the prompt from the turn's start, then the finished tool steps and lines merged into it) and `interrupted_turn: {id, started_at, at, iteration, enqueued_id}` (`enqueued_id`: the input the turn runs, so any process can tell which turn is running; a continue turn carries its offer turn's), written as the turn starts, at the loop's first iteration boundary and then at most every 10 s (`updated_at` moves with it). The turn's end saves the real messages without the mark. A worker that died without ending its turn (SIGKILL, a crash of the process) leaves the mark, and loses what came after its last written boundary: at most the last 10 s of steps, plus the step in flight. Each checkpoint write also saves the turn's metrics so far to `analytics.json` (tokens, speeds, timings, its finished tool calls), as the record a killed turn leaves: canceled, `cancellation_reason: worker_stopped`; the turn's own end writes over it. The next worker to start for the session (a `chi send`, `--resume`, `--attach`, the web), or a `--no-shared` REPL resuming it, settles the turn before anything else (`TurnCheckpoint.settle`, log `interrupted_turn_settled`): a note `the previous turn was cancelled (worker-stopped) after Ns` goes at the tail, the status goes idle and the mark goes; `last_turn` stays the previous turn's (a new one saved before the turn a `chi send --wait` woke the worker for would end that wait at once); the dead turn's end is recorded under its `enqueued_id` instead, in `<id>/input/settled/<enqueued_id>` (a `last_turn` record: canceled, `cancel_reason: worker_stopped`), which only a wait for that very turn reads (`chi send --queue --wait`, `chi send --wait ID --turn`, a continue's wait): it ends `canceled` with `cancel_reason: worker_stopped`, detail "its worker exited mid-turn", instead of timing out. A reply the turn wrote before its worker died is still its answer. The session shows the turn's prompt and steps with that note; the dead turn is not run again, and the model reads on its next turn that it was cut.
31
33
 
32
- **Stopping a worker:** `chi sessions stop ID` marks the session stopped (an empty `<id>/stopped` file, not a session.json field, which a worker saving at that moment would write over; a resume removes it), sends its worker TERM and waits (up to 10 s) until the worker has let go of `owner.lock`. A running turn is canceled over the Bridge first, with reason `session_stopped` (the turn's line ends "(session stopped)", its note says `cancelled (session-stopped)`, and a command it was running says "stopped as the session was stopped"; a bare signal to the worker makes it "stopped by a signal to chi"; an older worker takes the reason as `manual`); the TERM stops whatever still runs as a Stop with reason `signal` and saves it (a spawned worker traps TERM, HUP and INT: `WorkerSignals`; a turn not ended within 3 s, 0.5 s after a stop or a second signal, is cut and the session saved as it was), and a stopped worker still saves its kept turn, since the marker, not the saved status, says stopped. Any TERM, HUP or INT does the same to a worker outside a stop: it leaves (log `worker stop reason=signal`) and the next send wakes a new one, so a `chi --resume ID` right after spawns a fresh worker. That is also how to restart a worker still running an older chi, which the attached terminal reports when a command or a question dismiss gets a 404, and the web when an answer, a dismiss, a command or a cancel does (`501 not_supported`; `BridgeClient.stale_worker_message`). A command this chi has that the older worker doesn't (`/llm-context`, the web's `llm ctx` chip, against a 0.39.0 worker) says the same, not `not a session command` (`SessionCommands.builtin_name`); a typo keeps its `400 unknown_command`. The web's `POST /api/sessions/:id/stop` waits the same way, for 2 s. A command sent during a turn that waits for its end (`/model X`, `!cmd`; see *Typing during a turn* in [cli.md](cli.md)) and hasn't run when the worker leaves (a stop, a crash) is answered `command_ran {status: "dropped"}`, so no UI keeps showing it waiting.
34
+ **Stopping a worker:** `chi sessions stop ID` marks the session stopped (an empty `<id>/stopped` file, not a session.json field, which a worker saving at that moment would write over; a resume removes it), sends its worker TERM and waits (up to 10 s) until the worker has let go of `owner.lock`. A running turn is canceled over the Bridge first, with reason `session_stopped` (the turn's line ends "(session stopped)", its note says `cancelled (session-stopped)`, and a command it was running says "stopped as the session was stopped"; a bare signal to the worker makes it "stopped by a signal to chi"; an older worker takes the reason as `manual`); the TERM stops whatever still runs as a Stop with reason `signal` and saves it (a spawned worker traps TERM, HUP and INT: `WorkerSignals`; a turn not ended within 3 s, 0.5 s after a stop or a second signal, is cut and the session saved as it was), and a stopped worker still saves its kept turn, since the marker, not the saved status, says stopped. Any TERM, HUP or INT does the same to a worker outside a stop: it leaves (log `worker stop reason=signal`) and the next send wakes a new one, so a `chi --resume ID` right after spawns a fresh worker. That is also how to restart a worker still running an older chi, which the attached terminal reports when a command or a question dismiss gets a 404, and the web when an answer, a dismiss, a command or a cancel does (`501 not_supported`; `BridgeClient.stale_worker_message`). A command this chi has that the older worker doesn't (`/llm-context`, the web's `llm ctx` chip, against a 0.39.0 worker) says the same, not `not a session command` (`SessionCommands.builtin_name`); a typo keeps its `400 unknown_command`. The web's `POST /api/sessions/:id/stop` waits the same way, for 2 s (the page's Stop shows the stop at once), or for the body's `wait` up to the same 10 s (`SessionManager::STOP_WAIT`): `chi --remote NAME sessions stop` sends it, so it waits as long as here. A command sent during a turn that waits for its end (`/model X`, `!cmd`; see *Typing during a turn* in [cli.md](cli.md)) and hasn't run when the worker leaves (a stop, a crash) is answered `command_ran {status: "dropped"}`, so no UI keeps showing it waiting.
33
35
 
34
36
  **Deleting a session:** `chi sessions delete ID...`, `/exit --delete` in a terminal and the Web UI's delete all go through `SessionManager.delete_session`: it resolves a unique prefix, removes `<id>.json` and the whole `<id>/` dir, and returns what it removed. A session a plain REPL has open (`owner.lock` kind `tui`) is always refused. One a worker runs is refused unless the caller asks to stop it: then it stops the worker as `chi sessions stop` does and deletes once `owner.lock` is free (`--force` on the CLI, 10 s; the web page always, 2 s; `/exit --delete` after the worker agreed to exit, 10 s). A worker that outlives the wait leaves the session in place. The web's route is `DELETE /api/sessions/:id?force=true` (the page sends `force`; without it a live worker's session is 409 `worker_running`, as the CLI without `--force`; 200 `{status: "deleted", session_id, stopped}`; 409 `owned_by_tui` or `still_stopping`; 404).
35
37
 
data/docs/sub-agent.md CHANGED
@@ -164,6 +164,14 @@ An answer typed into `chi --attach` or the REPL counts as a parent's too when
164
164
  stdin isn't a terminal or an agent marker (`CLAUDECODE`, `AI_AGENT`,
165
165
  `CODEX_THREAD_ID`, `SAMAGOTCHI_PARENT_SESSION`) is set.
166
166
 
167
+ The same holds for another machine's session in this terminal (`chi --remote
168
+ NAME --attach ID`, `--resume ID`, `--dir DIR -p TEXT` from a pipe): the answers
169
+ go as chi answer's and are held to that machine's `guardrails.parent_answers`;
170
+ a question left waiting at the pipe's end is exit 3, its report read with that
171
+ machine's parent strategy (asked again then) and its commands spelled `chi
172
+ --remote NAME answer …`. For one turn and no terminal, use `chi --remote NAME
173
+ send --new --dir D -m TEXT --wait` (`--non-interactive` is this machine's only).
174
+
167
175
  Through `chi web` (`POST /api/sessions/:id/answer`, what chi answer uses against
168
176
  another machine's sessions) the answer keeps chi answer's mark: the route passes
169
177
  on `client_id: "cli:answer"` and drops any other id, so a forwarded answer can
data/docs/testing.md CHANGED
@@ -22,6 +22,9 @@ with two rules only, `no-undef` (a name used but never imported or declared: a d
22
22
  style pass: add a rule only for a bug class it would have caught. It runs in about 2 s, in CI's `npm test` job and in
23
23
  `rake release:check`; `rake lint` stays RuboCop alone (CI's rubocop job has no node). A name that is also a browser
24
24
  global (`name`, `status`, `open`…) passes even when its import is gone: give an export such a name only with care.
25
+ It also checks `spec/e2e/` for one thing, the Playwright twin of `Samagotchi/SpecSleep`: no `page.waitForTimeout`.
26
+ Wait for the state (a locator's `expect`, `data-phase`, an animation's end through `waitForFunction`); a pause that
27
+ is the behaviour (a pointer's dwell) carries `// eslint-disable-line no-restricted-syntax -- why`.
25
28
 
26
29
  CI's e2e job keeps the tests, chi and the fake model on the runner but runs Chromium in Playwright's image
27
30
  (`mcr.microsoft.com/playwright:v<version>-noble`, `playwright run-server` on the host network); the tests connect
@@ -66,11 +69,68 @@ either router adds its row there; a row where a UI differs on purpose (or a foun
66
69
  `repl` override and a `note`. The Ruby side runs each row with every core command (a new one fails its
67
70
  `CORE_ROUTES` check until listed there); a row's names that core lacks stand for plugin commands.
68
71
 
69
- A spec that waits for something uses `wait_until(timeout:)`, or `wait_until!(what, timeout:)` where no check
70
- follows (stubs, helper threads: it raises "timed out after N s waiting for <what>") — never a loop with no deadline.
72
+ ## Waiting and time
73
+
74
+ A spec waits *for* something and never sleeps to wait: a fixed pause is too short on a loaded runner (a parallel
75
+ run, CI) and too long on a fast one. The contract a new spec follows:
76
+
77
+ 1. **Never `sleep` to wait.** Wait for the condition: `wait_for("what") { condition }`, or
78
+ `eventually { expect(…) }` for a value. A `sleep` that *is* the behaviour (slowness, holding a connection,
79
+ separating mtimes) carries `# sleep-ok: why` on its line or the line above (for a call over several lines, its
80
+ first line). A bounded best-effort `wait_until`
81
+ whose timeout is fine ("at most 0.5 s, fine to cut short") carries `# wait-ok: why`; it must not become
82
+ `wait_for`, which would raise where the code moves on. A `Timeout.timeout` is a wait too (use `pop!`, `join!` or
83
+ `wait_for`); one that only guards a call that returns by itself against a hang carries `# timeout-ok: why`.
84
+ 2. **Time logic takes seams.** Intervals, graces and deadlines in lib take `clock:` / `sleeper:` / `interval:`
85
+ constructor arguments, and the unit spec moves the clock itself (`SessionAccess::Remote` is the model;
86
+ `IdleScheduler.new(interval:)`). Real time is used only in one smoke example per class. Two sessions or inbox
87
+ files that must sort apart get distinct times, not a pause: `Session.new_session(now:)`, `Session#save(now:)` and
88
+ `SessionInbox.write_input` / `write_note` / `write_output(now:)` take `now: next_stamp` (spec/support/stamps.rb:
89
+ a millisecond later on each call, starting a second in the past).
90
+ 3. **What a spec starts, it stops and waits for** before teardown: `join!(thread, what)`, `with_task_dir { … }`
91
+ (spec/support/task_dirs.rb: a tasks' folder of its own, its tasks stopped and their exit-status recorders joined
92
+ with `TaskRuntime.quiesce!`), a `stop` that joins (`IdleScheduler#stop`). Never only `Thread#kill`.
93
+ 4. **"Nothing happens" is shown by counting passes** (the worker's loop rounds, spec/support/worker_loop_rounds.rb;
94
+ a class's ticks), not by a pause.
95
+ 5. **A wall-clock bound** only as "not the full N s", with at least 5× headroom measured on the slow (x86) laptop.
96
+
97
+ The helpers are the bundle test kit's (`Samagotchi::Testing::Waiting`, [plugins.md](plugins.md#testing-a-bundle-experimental)),
98
+ so core and bundle specs share one copy; `SpecWaiting` (spec/support/waiting.rb) includes them in every example
99
+ group and also answers them as module calls (`SpecWaiting.pop!`, for a stubbed lib thread):
100
+
101
+ - `wait_for(what, timeout: 5, interval: 0.01, seen: nil) { … }` returns the block's first truthy value, or raises
102
+ `Waiting::TimedOut`. `seen:` (a callable) shows what the condition last saw, since a falsy block value tells
103
+ nothing.
104
+ - `join!(thread, what, timeout: 5)` and `pop!(queue, what, timeout: 5)`: `Thread#join` and `Queue#pop` with the
105
+ same deadline and message. A gate a stubbed lib thread waits on takes `timeout: 30`: it only ends a hang.
106
+ - `wait_until(timeout:) { … }` returns the block's last value (falsy at the deadline): for a value the example
107
+ checks after, or a `wait-ok` wait.
108
+ - `eventually(timeout: 5) { expect(…) }` (spec only) retries until the expectations hold, then re-raises the last
109
+ failure prefixed `still failing after 5 s:`, so the matcher's own expected/got is in the message.
110
+
111
+ One formatter builds the failure text of `wait_for`, `join!` (which adds "to finish") and `pop!`:
112
+
113
+ ```
114
+ timed out after 5 s waiting for task 20261023-0a1b to end
115
+ timed out after 10.0 s (5 s × SPEC_TIME_SCALE 2) waiting for task 20261023-0a1b to end; last saw: "running"
116
+ ```
117
+
118
+ **`SPEC_TIME_SCALE`** (default 1, at least 1; CI's `rspec`, `rspec-macos` and `bundles` jobs set 2) multiplies
119
+ every helper deadline, explicit `timeout:` values included: a positive wait only gets more headroom, and a passing
120
+ run is no slower. Set it locally to reproduce a CI timing (`SPEC_TIME_SCALE=2 bundle exec rspec FILE`). It does not
121
+ scale the lib's own timeouts a spec passes in (`wait: 0.05`, `grace: 0.1`): those are the code's deadlines, and a
122
+ flaky one gets a `clock:` seam instead. A helper's own spec pins the scale (stub `Waiting.time_scale`). Keep
123
+ explicit helper timeouts at 30 or below: 60 s on CI, under the per-example limit.
124
+
71
125
  Every example also runs under a 120 s limit (`spec_helper`'s `around`; `integration`, `macos_build`, `warmup` and
72
126
  `recap` examples are exempt), so a hang fails its example instead of the run. `SPEC_EXAMPLE_TIMEOUT=N` changes the
73
- limit, `0` turns it off (a debugger session).
127
+ limit, `0` turns it off (a debugger session). The limit is not scaled.
128
+
129
+ **The cops.** `Samagotchi/SpecSleep` (an untagged `sleep` in a spec) and `Samagotchi/SilentWait` (a `wait_until`
130
+ whose value is dropped, an example's last line included, `expect(wait_until { … }).to be(true)`, or an untagged
131
+ `Timeout.timeout`) live in `rubocop/` and run in `rake lint`, so CI fails on them. RSpec counts `wait_for`,
132
+ `eventually`, `join!` and `pop!` as an example's expectation (`RSpec/NoExpectationExample`), so a wait can be an
133
+ example's last line. What the cops can't see, `script/affected-specs` hints at (`timing:`, below).
74
134
 
75
135
  ## Bundle specs: the test kit
76
136
 
@@ -140,7 +200,11 @@ words the REPL prints, pick it by a rule), then the other name matches (a spec n
140
200
  `toolkit.rb`; then one beside the file, so `plugin/context.rb` picks `spec/samagotchi/plugin/*context*` before
141
201
  `spec/guardrails/context_spec.rb`), then the specs that name a changed file's class. The default range is the merge-base with main (or origin/main) plus uncommitted and untracked files.
142
202
  The Ruby specs go to stdout; the node tests to run (`npm test` targets), "web changed: also run npm run e2e" (with `npm run lint` too when a frontend module changed) and a
143
- hint for a hub class (named in too many specs to list) go to stderr. Nothing printed means nothing matched.
203
+ hint for a hub class (named in too many specs to list) go to stderr, and so do the `timing:` hints, from the lines a
204
+ changed spec adds (`git diff -U0` against the base; an untracked spec, all its lines): a `Thread.new`, `.start`,
205
+ `spawn` or `fork` with no `join!`, `wait_for`, `with_task_dir` or `Process.wait` added in the same file, and a
206
+ sub-second `be < 0.x` bound ([Waiting and time](#waiting-and-time), rules 3 and 5). Nothing printed means nothing
207
+ matched.
144
208
 
145
209
  It finds a change's own specs, not the regressions it causes elsewhere. So before committing, run the full suite
146
210
  (`bundle exec parallel_rspec -n 8 spec lib/samagotchi/bundles/*/spec`), unless your task says the coordinator runs it; the coordinator runs it at merge
@@ -892,6 +892,10 @@ module Samagotchi
892
892
  client_id = fetched(parsed, "client_id")
893
893
  # --no-interrupt: the turn runs with the raised iteration limit.
894
894
  no_interrupt = fetched(parsed, "no_interrupt") == true
895
+ # Who wrote it when it isn't the owner (chi web stamps it from the
896
+ # request's principal; the Bridge is loopback and trusts its caller).
897
+ author = fetched(parsed, "author")
898
+ author = nil unless author.is_a?(String) && !author.empty?
895
899
  if sid.to_s.strip.empty? || prompt.to_s.strip.empty?
896
900
  return [{ "Allow" => "POST" }, 400,
897
901
  { error: "missing_fields", detail: "session_id and prompt are required" }]
@@ -935,7 +939,7 @@ module Samagotchi
935
939
 
936
940
  queued_behind_turn = Delivery.queue?(delivery) && !@accumulator.current_turn.nil?
937
941
  enqueue_turn(prompt: prompt, client_id: client_id, enqueued_id: enqueued_id,
938
- no_interrupt: no_interrupt, images: images, delivery: delivery).tap do |ok|
942
+ no_interrupt: no_interrupt, images: images, delivery: delivery, author: author).tap do |ok|
939
943
  next unless ok
940
944
 
941
945
  enqueued_event = { type: :turn_enqueued, enqueued_id: enqueued_id, client_id: client_id, prompt: prompt.to_s }
@@ -1082,9 +1086,9 @@ module Samagotchi
1082
1086
 
1083
1087
  # Write a turn into this session's input dir, reusing the file IPC the
1084
1088
  # worker polls. Never calls run_turn across the boundary.
1085
- def enqueue_turn(prompt:, client_id: nil, enqueued_id: nil, no_interrupt: false, images: [], delivery: nil)
1089
+ def enqueue_turn(prompt:, client_id: nil, enqueued_id: nil, no_interrupt: false, images: [], delivery: nil, author: nil)
1086
1090
  SessionInbox.write_input(session_dir, prompt: prompt, client_id: client_id, enqueued_id: enqueued_id,
1087
- no_interrupt: no_interrupt, images: images, delivery: delivery)
1091
+ no_interrupt: no_interrupt, images: images, delivery: delivery, author: author)
1088
1092
  end
1089
1093
 
1090
1094
  # ── HTTP plumbing ────────────────────────────────────────────────────────
@@ -31,8 +31,9 @@ module Samagotchi
31
31
  # WebBridgeClient), where the client stays the same object across
32
32
  # workers: +epoch_watch+ (a frame whose id has another epoch than the
33
33
  # cursor's is a new worker: worker_changed first, the client's key
34
- # updated), +announce_retries+ (stream_retrying once a reconnect fails,
35
- # stream_resumed at the next frame), +idle_timeout+ (no byte, not even
34
+ # updated), +announce_retries+ (stream_retrying once a reconnect fails
35
+ # and rediscovery neither moved nor ended the stream, stream_resumed
36
+ # once a stream answers again), +idle_timeout+ (no byte, not even
36
37
  # the Bridge's 15 s ping, for that long: a half-open connection,
37
38
  # reconnected with the cursor).
38
39
  class EventStream
@@ -125,7 +126,8 @@ module Samagotchi
125
126
  next
126
127
  end
127
128
 
128
- announce_retry
129
+ # Rediscovery first: a stopped session or a new worker is no
130
+ # retry to announce (a stop prints its line alone).
129
131
  case rediscover_worker
130
132
  when :moved
131
133
  failures = 0
@@ -133,6 +135,7 @@ module Samagotchi
133
135
  next
134
136
  when :gone then return finish(@reason || "unreachable")
135
137
  end
138
+ announce_retry
136
139
 
137
140
  failures += 1
138
141
  return finish(@reason || "unreachable") if failures > @reconnect_delays.size
@@ -153,6 +156,9 @@ module Samagotchi
153
156
  return :unknown_session if status == 404
154
157
  return :unreachable unless status == 200
155
158
 
159
+ # Back as soon as the stream answers: a replay may bring nothing
160
+ # (the session was quiet), and the next frame could be minutes away.
161
+ announce_resumed
156
162
  yielded = false
157
163
  parser = SSEParser.new
158
164
  loop do
@@ -163,7 +169,6 @@ module Samagotchi
163
169
  event = parse(frame[:data])
164
170
  next unless event
165
171
 
166
- announce_resumed
167
172
  watch_epoch(frame[:id]) if @epoch_watch
168
173
  @last_event_id = frame[:id] if frame[:id]
169
174
  yielded = true
@@ -165,9 +165,12 @@ module Samagotchi
165
165
  # @param no_interrupt [Boolean] run the turn with the raised iteration limit
166
166
  # @param images [Array<Hash>] refs ({file:, name:}) to images already in
167
167
  # the session's images/
168
- def post_turn(prompt:, client_id: nil, no_interrupt: false, images: nil, delivery: nil)
168
+ # @param author [String, nil] who wrote it when it isn't the session's
169
+ # owner; sent only when given (SessionManager.deliver_turn)
170
+ def post_turn(prompt:, client_id: nil, no_interrupt: false, images: nil, delivery: nil, author: nil)
169
171
  body = { session_id: @session_id, prompt: prompt, client_id: client_id }
170
172
  body[:no_interrupt] = true if no_interrupt
173
+ body[:author] = author if author
171
174
  body[:images] = images if images && !images.empty?
172
175
  body[:delivery] = delivery unless delivery.nil? || Delivery.next_step?(delivery)
173
176
  body[:deadline] = deadline
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: samagotchi-system
3
- version: 0.103.0
3
+ version: 0.104.0
4
4
  scope: system
5
5
  description: Default system memories — identity, self map, config modification protocol, memory guide and the delegated-session rules
6
6
  files: