samagotchi 0.62.0 → 0.64.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +78 -1
- data/README.md +4 -1
- data/bin/chi +3 -2
- data/docs/architecture.md +12 -2
- data/docs/cli.md +20 -9
- data/docs/configuration.md +16 -9
- data/docs/context.md +13 -3
- data/docs/hooks.md +6 -3
- data/docs/internals/engine.md +10 -3
- data/docs/memory.md +1 -1
- data/docs/plugins.md +96 -2
- data/docs/releasing.md +1 -1
- data/docs/sessions.md +11 -8
- data/docs/sub-agent.md +27 -4
- data/docs/testing.md +13 -4
- data/lib/samagotchi/answer_command.rb +5 -1
- data/lib/samagotchi/answer_refs.rb +18 -26
- data/lib/samagotchi/bridge/turn_accumulator.rb +1 -0
- data/lib/samagotchi/bridge.rb +41 -5
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +22 -0
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +406 -0
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +104 -0
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +66 -0
- data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +60 -0
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -1
- data/lib/samagotchi/bundles/loop-guard/manifest.yml +2 -2
- data/lib/samagotchi/bundles/loop-guard/plugin.rb +17 -6
- data/lib/samagotchi/bundles/slow-commands/manifest.yml +2 -2
- data/lib/samagotchi/bundles/slow-commands/plugin.rb +5 -3
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +3 -3
- data/lib/samagotchi/bundles/system/memory_guide.md +1 -1
- data/lib/samagotchi/child_reports.rb +7 -2
- data/lib/samagotchi/cli/parent_wait.rb +9 -2
- data/lib/samagotchi/composer_route.rb +90 -0
- data/lib/samagotchi/config.rb +6 -3
- data/lib/samagotchi/context_absorber.rb +25 -6
- data/lib/samagotchi/continue_offer.rb +17 -3
- data/lib/samagotchi/delivery.rb +13 -8
- data/lib/samagotchi/engine.rb +2 -0
- data/lib/samagotchi/host_config.rb +6 -6
- data/lib/samagotchi/host_model.rb +2 -2
- data/lib/samagotchi/idle_recap.rb +2 -3
- data/lib/samagotchi/image_store.rb +8 -0
- data/lib/samagotchi/interrupted_turn.rb +7 -3
- data/lib/samagotchi/last_turn.rb +5 -2
- data/lib/samagotchi/pending_input_queue.rb +10 -3
- data/lib/samagotchi/plugin/attached_context.rb +7 -4
- data/lib/samagotchi/plugin/sessions.rb +27 -5
- data/lib/samagotchi/recap_setup.rb +3 -3
- data/lib/samagotchi/reply_wait.rb +171 -6
- data/lib/samagotchi/row_refs.rb +3 -4
- data/lib/samagotchi/send_command.rb +63 -19
- data/lib/samagotchi/session.rb +12 -0
- data/lib/samagotchi/session_commands.rb +1 -1
- data/lib/samagotchi/session_inbox.rb +63 -7
- data/lib/samagotchi/session_manager.rb +49 -20
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -3
- data/lib/samagotchi/terminal_ui/attached_loop.rb +73 -35
- data/lib/samagotchi/terminal_ui/formatting.rb +3 -19
- data/lib/samagotchi/terminal_ui.rb +103 -68
- data/lib/samagotchi/tool_activity.rb +5 -5
- data/lib/samagotchi/tools/delegate.rb +28 -1
- data/lib/samagotchi/tools/delegate_cursor.rb +9 -4
- data/lib/samagotchi/tools/delegate_wait.rb +17 -5
- data/lib/samagotchi/tools/execute.rb +33 -8
- data/lib/samagotchi/turn_checkpoint.rb +9 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +6 -4
- data/lib/samagotchi/web/public/activity.js +2 -2
- data/lib/samagotchi/web/public/app.js +53 -91
- data/lib/samagotchi/web/public/card.js +73 -23
- data/lib/samagotchi/web/public/chains.js +6 -4
- data/lib/samagotchi/web/public/command_view.js +39 -35
- data/lib/samagotchi/web/public/commands.js +8 -8
- data/lib/samagotchi/web/public/composer.js +3 -3
- data/lib/samagotchi/web/public/copy.js +7 -15
- data/lib/samagotchi/web/public/diff_view.js +20 -23
- data/lib/samagotchi/web/public/format.js +0 -48
- data/lib/samagotchi/web/public/images.js +12 -16
- data/lib/samagotchi/web/public/question_cards.js +3 -2
- data/lib/samagotchi/web/public/read_view.js +10 -9
- data/lib/samagotchi/web/public/scope.js +23 -0
- data/lib/samagotchi/web/public/timing.js +2 -8
- data/lib/samagotchi/web/public/turn_html.js +119 -59
- data/lib/samagotchi/web/public/turn_model.js +13 -3
- data/lib/samagotchi/web/public/turn_view.js +73 -116
- data/lib/samagotchi/web/public/vendor/directive.js +8 -0
- data/lib/samagotchi/web/public/vendor/directives/unsafe-html.js +8 -0
- data/lib/samagotchi/worker.rb +53 -35
- data/lib/samagotchi/worker_inbound.rb +12 -11
- metadata +9 -1
data/docs/sessions.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Sessions are plain files — no DB. Each session is `~/.local/state/samagotchi/sessions/<uuid>.json` (XDG-aware via `XDG_STATE_HOME`) plus a sidecar dir `<uuid>/` with `input/`/`notes/`/`output/`/`owner.lock`/`bridge.json` (`Session.session_dir`). Besides the messages and turn state, the JSON keeps `used_memory_names` (what the session read), `preloaded_memory_names` (`--memory`) and `muted_memory_names` (`--mute`), the last two written before the worker starts so a respawn builds the same prompt, `prompt_notes`, the model notes the system prompt carried at its last build (`[{name, scope, chars, digest}]`, see [Memory](memory.md#model-notes)), `parent_id`, the session that delegated this one (see *Delegating*; `null` otherwise), and `continues`, the session this one continues (the previous link of a chain, see *Session chains*; `null` otherwise); files from before those keys read as empty lists, no parent and no previous link. `model_name` is the resolved model ref (an alias applied, `host:id` when it names a host), so a resumed session keeps its model when an alias is retargeted; `model_typed` is the name it was given as when that differs (an alias), for the `models:` lookup after a resume (`null` otherwise).
|
|
4
4
|
|
|
5
|
+
**Input files:** a message for a session's worker waits in `input/<timestamp>.json` (`SessionInbox.write_input`: `prompt`, `client_id`, `enqueued_id`, and `images`, `no_interrupt` and `delivery` when they apply; `delivery` is written only for `"queue"`). While a turn runs, the worker merges them at each step boundary in name order, except a queued one or one with images: those wait for the turn's end and run one turn each, in name order, and a file after them still merges (`Worker#pending_input_drain`). A queue is sent only to a worker whose `bridge.json` names the `delivery` feature; an older one would steer it, so `SessionManager.deliver_turn` refuses it (409 `delivery_unavailable`), and any other 4xx the Bridge answers is a refusal too, never a fallback file (except a 413, a body over its size limit: that one still goes in as a file, its `delivery` kept). **Reply files:** a turn that ends with visible text leaves its reply in `output/<timestamp>.txt`, or `output/<timestamp>-<enqueued_id>.txt` when the turn ran an input file with an id (`SessionInbox.write_output`), and the session's `last_turn` keeps that `enqueued_id`; so a wait for one queued message (`chi send --queue --wait`, `ReplyWait`'s `turn:`) finds its own turn's reply or end, never another turn's. The names still sort by time, and every reader takes `*.txt`.
|
|
6
|
+
|
|
5
7
|
**Retention (file-based, opt-out via env):**
|
|
6
8
|
|
|
7
9
|
| Env | Default | Purpose |
|
|
@@ -23,9 +25,9 @@ A session is deleted if **expired by age OR overflow by count** (unless `keep_st
|
|
|
23
25
|
|
|
24
26
|
**A worker outlives whoever started it:** a spawned worker leaves its spawner's process tree (it is forked again, in a session of its own, and reparented to init), so closing the terminal of a `chi web`, or a tool tearing down a background `chi send --wait` and its children, no longer takes the worker and its turn with it.
|
|
25
27
|
|
|
26
|
-
**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}
|
|
28
|
+
**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 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.
|
|
27
29
|
|
|
28
|
-
**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 (
|
|
30
|
+
**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.
|
|
29
31
|
|
|
30
32
|
**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 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` (200 `{status: "deleted", session_id, stopped}`; 409 `owned_by_tui` or `still_stopping`; 404).
|
|
31
33
|
|
|
@@ -153,21 +155,22 @@ cancel during its thinking, it said it had never been asked.
|
|
|
153
155
|
`chi send` is the other half of `chi note`: the text goes in as your message, the same as typing it in the attached terminal or the web composer, so a turn runs.
|
|
154
156
|
|
|
155
157
|
```sh
|
|
156
|
-
chi send [-m TEXT] [--image PATH]... [--cut] (ID|PREFIX)...
|
|
158
|
+
chi send [-m TEXT] [--image PATH]... [--cut | --queue] (ID|PREFIX)...
|
|
157
159
|
chi send -m "is this the same bug?" 3fa2 # a message
|
|
158
160
|
chi send --cut -m "stop, wrong file" 3fa2 # cut the running turn's long thinking
|
|
161
|
+
chi send --queue -m "then update the docs" 3fa2 # a turn of its own after the running one
|
|
159
162
|
chi send --image shot.png -m "why is this red?" 3fa2 # with an image
|
|
160
163
|
pbpaste | chi send -m "is this the same bug?" 3fa2 # the clipboard quoted above the message
|
|
161
164
|
pbpaste | chi send 3fa2 # the clipboard is the message
|
|
162
165
|
```
|
|
163
166
|
|
|
164
167
|
- With both stdin and `-m`, stdin is context: each line becomes a `> ` quote (as web annotations quote a selection), then a blank line, then the message. With only one, it goes in as is. A terminal on stdin is ignored. Both empty is a usage error; over 16 KiB is refused, never cut.
|
|
165
|
-
- It goes through the same path as the web composer (`SessionManager.deliver_turn`): the worker's Bridge when it is up, so every attached UI shows it as your message (client id `cli:send`, shown like any user message); the input file when the worker is on its way out. During a running turn it is merged into that turn at the next step, like a message typed then, and cuts nothing. `--cut` asks for a cut, as `/cut TEXT` in a terminal does: a generation that has streamed only thinking for `steer.cut_after` seconds (per host/model: `steer_cut_after`) is cut (now, or once its thinking passes that), and the step starts again with the message. To an idle session `--cut` sends normally. `--cut --wait` into a running turn is refused like any `--wait` there (`busy: a turn is running`).
|
|
168
|
+
- It goes through the same path as the web composer (`SessionManager.deliver_turn`): the worker's Bridge when it is up, so every attached UI shows it as your message (client id `cli:send`, shown like any user message); the input file when the worker is on its way out. During a running turn it is merged into that turn at the next step, like a message typed then, and cuts nothing. `--cut` asks for a cut, as `/cut TEXT` in a terminal does: a generation that has streamed only thinking for `steer.cut_after` seconds (per host/model: `steer_cut_after`) is cut (now, or once its thinking passes that), and the step starts again with the message. To an idle session `--cut` sends normally. `--cut --wait` into a running turn is refused like any `--wait` there (`busy: a turn is running`). `--queue` (not with `--cut`, a usage error) keeps the message for after the running turn: it runs as a turn of its own then (as `/queue TEXT` does), and a plain message sent after it still goes in at the running turn's next step. To an idle session it runs at once (it still goes as a queue, so a turn another client starts first never takes it as a steer). A worker older than queueing would steer it instead, so it is refused (to an idle session too) (`refused: this session's worker predates queueing, …; restart it`, exit 1) and nothing is sent; `chi sessions restart ID` and send again.
|
|
166
169
|
- A session with no worker gets one started (like `--attach` or the web composer). A session open in a plain REPL (`--no-shared`) refuses it. Only sessions on this machine: Bridges listen on 127.0.0.1.
|
|
167
170
|
- Fire and forget: it returns once the message is queued and never prints the answer; that shows in whatever is attached. A guardrail "ask" waits for a UI to answer it, so with nothing attached the turn stalls there until one attaches (`chi --attach ID`).
|
|
168
171
|
- `--image PATH` (repeatable, up to 20) sends images with the message, as the web composer's chips do. Each file is read once before anything is sent (bmp, tiff and heic converted to png, large ones downscaled, as for `@path`); a missing file or one that isn't an image stops the send (exit 1; with `--wait --format json`, an `error` line) and nothing is sent; more than 20 is a usage error (exit 2). Each session gets its own copy in `<session>/images/`. Text is still required (`-m` or stdin; context alone counts), also with `--wait`. The session's model must see images: one known not to (the host's model list says text-only, `vision: false` in `models:` or `hosts:`, a native llama.cpp without a vision model) is refused before anything is sent, with the reason (`<id> refused: gemma can't take images (…); send text only or switch the model (/model)`); the other sessions of the same send still get it, and the exit is 1. With `--new` nothing is started. When chi can't tell (a native host whose `/props` doesn't answer, a list that doesn't say), the message is sent as before and a text-only model fails the turn in the session. A running turn doesn't take images mid-turn: the message runs as the next turn, and the line says `(runs after the current turn)`. With `--new` the session starts idle with the message as its preview, then the message goes in as its first turn once its worker is up (`<id> started with 1 image`); if the worker doesn't come up in 5 s the session is kept, with its id on the `failed:` line.
|
|
169
172
|
- A session command runs as the command, as typed in a terminal: `chi send -m "/model qwen" 3fa2` switches the model (`sent as a session command`), and the model never sees the line. What counts is what the session's worker knows (the built-ins, `!cmd`, its bundles' commands); an unknown `/word` (`/usr/bin/env is missing`) goes in as a message. A UI-only command (`/stats`, `/exit`, `/recap`, …) is refused instead: the worker doesn't run it, so the line would reach the model as a prompt (`<id> refused: /stats is a terminal and web command; use chi sessions stats <id8>`; exit 1, and nothing is sent). With `--new` a line starting with `/` or `!` starts the session idle and goes to its worker once it is up, which decides. With `--wait` a command has no reply to wait for: exit 0 at once, `<id> sent as a session command: no reply to wait for …` on stderr (`{"status":"command",…,"detail":…}` with `--format json`); its output shows where the session is open.
|
|
170
|
-
- One line per session: `sent`, `sent as a session command`, `sent with 2 images`, `sent (goes in at the running turn's next step)`, with `--cut` `sent (cut in now)`, `sent (cuts in once the thinking passes 20 s)` or `sent (cutting is off; goes in at the next step)`, `sent (started its worker)`, `refused: …` or `failed: …`. Exit 0 when all were sent, 1 when any was refused, failed or not found, 2 for a usage error. There is no `--all`.
|
|
173
|
+
- One line per session: `sent`, `sent as a session command`, `sent with 2 images`, `sent (goes in at the running turn's next step)`, with `--cut` `sent (cut in now)`, `sent (cuts in once the thinking passes 20 s)` or `sent (cutting is off; goes in at the next step)`, with `--queue` `sent (queued: runs after the current turn)`, `sent (the running turn had ended: it runs now)` or `sent (nothing running: it runs now)`, `sent (started its worker)`, `refused: …` or `failed: …`. Exit 0 when all were sent, 1 when any was refused, failed or not found, 2 for a usage error. There is no `--all`.
|
|
171
174
|
|
|
172
175
|
The Automator action above works for messages too: swap its last line for `pbpaste | "$chi" send -m "what do you make of this?" $(print -r -- "$picked" | cut -f1)`.
|
|
173
176
|
|
|
@@ -190,10 +193,10 @@ chi send --new --continues last:3fa2 -m "new day" # the next link of 3fa2's
|
|
|
190
193
|
(`SAMAGOTCHI_THINKING_LEVEL=low chi send --new …`) only reaches the worker this call spawns: when the session's
|
|
191
194
|
worker is woken later (by `chi web`, a delivery) without that env, the level is gone. Use `--thinking`.
|
|
192
195
|
- `--new` prints `<full id> started` on stdout. With `--wait` that line (and the `sent` line for an existing session) goes to stderr, so stdout is exactly the answer: the reply of the turn the message started, in full. It is the worker's `output/` file, the same one `delegate` reads; a turn that ends with only tool calls and no text has none.
|
|
193
|
-
- `--wait` takes one session: `--new` or one id. A session with a running turn is refused (`busy: a turn is running; wait or attach`, exit 1): the message would
|
|
196
|
+
- `--wait` takes one session: `--new` or one id. A session with a running turn is refused (`busy: a turn is running; wait or attach`, exit 1): the message would merge into it, and its reply would come back as the answer. `--queue --wait` is the way in: the message runs as a turn of its own after the running one, and the wait is for that turn alone (the ack's `enqueued_id` names its reply file and its `last_turn`), so the running turn's reply and end, and a question it asks meanwhile, are not the answer (it keeps waiting through them; its `--timeout` still applies); a question its own turn asks is (exit 3). Two queued waits each get their own turn's answer. A session waiting for an answer is still refused. `--format json` adds `delivery`: `queue`, or `next_step` when the running turn ended before the message went in (it then ran at once, and the answer is still its own).
|
|
194
197
|
- Exit codes with `--wait`: 0 answered; 3 the turn waits for an answer from you (a question, a hook's question, a guardrail approval, or the step-limit question: see [chi as a sub-agent](sub-agent.md#the-step-limit)), and the session keeps waiting: stderr gets the whole question (`waiting for an answer (approval): …`, its text, the numbered options, whether free text is allowed) and how to answer it (`chi answer ID --question QID --option N`, or `chi --attach ID`, the web); 4 `--timeout S` passed with the turn still running (it goes on; wait again); 1 the turn ended without a reply (canceled, failed or empty), the worker failed or vanished, or the session was stopped; 130 on Ctrl-C, which leaves the turn running (`still running: chi --attach ID`). There is no default timeout.
|
|
195
198
|
- `--format json` (with `--wait`) prints one JSON object on stdout however the wait ends, for a script or a parent agent (see [chi as a sub-agent](sub-agent.md)): `{"status":"answered","session_id":…,"text":…}`; `{"status":"question",…,"question":{"id","kind","header","text","options","multi_select","allow_freeform","approval"},"answer_with":"chi answer ID --question QID --option N"}`, where `kind` is `question` (the model's), `hook` or `approval` (with the tool, command, cwd, rule, reason and offered scopes); `{"status":"running",…,"detail":…}` (exit 4, and on Ctrl-C); and `failed`, `canceled`, `no_answer`, `error`, `worker_gone` or `stopped` with a `detail` line (exit 1); `failed` adds `error_kind` and `retryable`, `canceled` adds `cancel_reason` and `stopped_by`, when known. A failure before the wait (no such session, a busy one) is `error` too. A usage error (exit 2) prints no JSON.
|
|
196
|
-
- `--wait ID` with no message (no `-m`, nothing piped) sends nothing: it waits for the session's next reply and prints and exits as above. A running session is fine (that is the point), so after exit 3, 4 or 130 an agent waits again this way while the user answers in the web or `chi --attach`; the question pending when it starts doesn't count again. An idle session with no worker waits until something wakes one. With `--new`, or two ids, it is a usage error (exit 2).
|
|
199
|
+
- `--wait ID` with no message (no `-m`, nothing piped) sends nothing: it waits for the session's next reply and prints and exits as above. With a turn running, that turn's reply (its `interrupted_turn` mark names it), not a turn's queued behind it; with the step-limit question standing, the reply of the turn its Continue runs. With neither (or a worker whose `bridge.json` lacks `turn_ids`), the next reply of any turn. A running session is fine (that is the point), so after exit 3, 4 or 130 an agent waits again this way while the user answers in the web or `chi --attach`; the question pending when it starts doesn't count again. An idle session with no worker waits until something wakes one. With `--new`, or two ids, it is a usage error (exit 2).
|
|
197
200
|
- `--continues ID` (with `--new`) starts the next link of that session's chain instead (see *Session chains*): in its folder, on its model, LLM context and thinking level, so `--dir`, `--model`, `--llm-context*` and `--thinking` are refused beside it; `ID` may be a prefix or `last:ID`, the chain's latest link. Without a message the new link starts idle; `--wait` then has nothing to wait for (a usage error). It prints `<full id> started (continues 3fa2c1d0)`, with `; 2 delegates moved` inside the parentheses when open delegates moved to it; a refusal (continued already, naming the next link; an open delegate that can't move; a turn running) is `chi send: refused: …`, exit 1.
|
|
198
201
|
- The 16 KiB cap applies: `git diff | chi send --new …` on a big diff is refused; name the branch in the message instead and let the session read it.
|
|
199
202
|
- A session nobody attaches to stalls at its first question or guardrail ask until someone answers it (`chi answer`, the web, `chi --attach`), and its worker stays up meanwhile: it doesn't idle-exit while a question waits (`chi sessions list` shows it as `waiting`; `chi sessions stop ID` frees it). Otherwise it idle-exits after `session.idle_exit_minutes` like any worker. These are ordinary sessions: delete them like any other.
|
|
@@ -220,7 +223,7 @@ A session can **continue** an earlier one: "the next day of the same routine" (a
|
|
|
220
223
|
|
|
221
224
|
The `delegate` tool hands a task to a **child session**: an ordinary chi session in a worker of its own, started in the parent's `working_directory` with the task, verbatim, as its first user message, on the parent's model unless the call names one (`model:` takes a name or an alias, resolved as `--model` is; an unknown host comes back as the tool's `Error:` line; an id the host's saved model list doesn't have starts the child with a `Warning: host 'h' doesn't list model 'x' (did you mean: …?)` line in the result). Children run in parallel and only their **final reply** comes back to the parent: the child's newest `output/<timestamp>.txt` file, which the worker writes at the end of each turn that produced visible text, cut head-and-tail like an `execute` result. Nothing of the child's trace enters the parent's context. Nothing is hidden: a child shows in `chi sessions list` (`↳ <parent>`), the web (folded into its parent's card on the strip and in all sessions, whose `▸ N delegates` chip lists the delegates at any depth, see Web UI above; a card of its own with a `↳` chip when its parent isn't listed; `delegated by` in its info bar; the parent's info bar carries a `⑂ N` chip counting its delegates, with how many run, wait, are done, failed, stopped or idle in the tooltip and the waiting ones named in the attention colour; a click opens all sessions), the terminal's status row (a parent's `⑂ 2 running · 1 waiting`, see [Status Line](cli.md#status-line)) and the `list_sessions` tool (`child`; the parent shows as `parent` in the child's own list); the user can `chi --attach <child id>` and steer it mid-run, and `chi send` and `send_note` reach it like any session.
|
|
222
225
|
|
|
223
|
-
- `delegate(task, model:, session:, cwd:, wait:, timeout:)`: starts a child and, by default, waits for its reply. `wait: false` is the usual way for work that takes a while, several children side by side, or to keep talking with the user: it returns at once, the parent ends its turn or works on, and the reply comes back by itself as a *delegate report* (below), in a turn of its own if the parent is idle. The tool descriptions tell the model so, and to wait (`wait: true`, `delegate_result`) only when its turn can't go on without the reply. `timeout`, default 600 s, leaves a slow child running; 0 checks once without waiting. With `session:` (a child's id or prefix) the task goes to that child as a follow-up instead, delivered like `chi send` (client id `delegate:<parent short id>`, shown as `delegate>` in the child's UIs); only this session's children take one.
|
|
226
|
+
- `delegate(task, model:, session:, cwd:, wait:, timeout:)`: starts a child and, by default, waits for its reply. `wait: false` is the usual way for work that takes a while, several children side by side, or to keep talking with the user: it returns at once, the parent ends its turn or works on, and the reply comes back by itself as a *delegate report* (below), in a turn of its own if the parent is idle. The tool descriptions tell the model so, and to wait (`wait: true`, `delegate_result`) only when its turn can't go on without the reply. `timeout`, default 600 s, leaves a slow child running; 0 checks once without waiting. With `session:` (a child's id or prefix) the task goes to that child as a follow-up instead, delivered like `chi send` (client id `delegate:<parent short id>`, shown as `delegate>` in the child's UIs); only this session's children take one. The wait (`wait: true`, a later `delegate_result`) and the delegate report are for the reply of the child's turn the follow-up went to: its running turn, which it merges into, or the turn of its own it runs when the child is idle (`turn` in `delegates.json`, set when the child's worker names `turn_ids`), not a newer reply of a turn queued behind it. A follow-up merged as a steer into a turn the child started at that moment (a wake, a reminder, another message) gets that turn's reply or question. A child on its first turn, or with an older worker, gives its next reply, as before.
|
|
224
227
|
- `cwd:` starts the child in another folder of the parent's repository: a linked worktree (`git worktree add ../<repo>-<task> -b <branch>`, which the model runs with `execute` first, under guardrails; chi runs no git here) or a subfolder; a relative path is taken from the parent's folder. The folder must exist, have the parent's project root (the same common git dir, `MemoryPaths.project_root`) and be in a work tree: a git dir (`.git`, `.git/objects`, a bare layout's `.bare`) or a bare layout's container is refused (`git rev-parse --is-inside-work-tree`, read-only, decides). Outside git only a subfolder of the parent's folder is allowed. Otherwise the tool returns `Error: cwd must be a folder of this session's repository …` or `… is not in a work tree …`. A follow-up (`session:`) refuses `cwd:`: the child keeps its folder. The child's guardrails measure from its own worktree, and project memories, `list_sessions` and the web's project are the same as the parent's.
|
|
225
228
|
- `delegate_result(session:, timeout:)`: waits for a child's next reply: the one named, or the newest running child (`timeout` as for `delegate`: 0 checks once and returns at once). The reply comes back once; a later call waits for the next one. After `wait: false` it isn't needed: the report brings the reply.
|
|
226
229
|
- A wait that timed out or was canceled still gets its delegate report: neither moves the parent's cursor, so the child's reply comes as a report when its turn ends. The result says so (`chi brings its reply here by itself as a delegate report …`; with `queue`, that it joins the next step or turn) and that `delegate_result` waits again only if the turn needs the reply now. A parent that gets no reports (`session.delegate_reports: off`, a `--no-shared` REPL, `--non-interactive`) is told `delegate_result <id> waits again` instead, and `wait: false`'s result says `delegate_result waits for its reply`.
|
data/docs/sub-agent.md
CHANGED
|
@@ -13,17 +13,28 @@ worker keeps the turn paused. The parent answers with `chi answer`, from what it
|
|
|
13
13
|
knows or after asking its own user, and chi goes on from there. The model gets
|
|
14
14
|
the answer as its tool result, as if you had answered in the web.
|
|
15
15
|
|
|
16
|
+
The other direction, chi sending a part of its work to a Claude Code session
|
|
17
|
+
you already work with (to validate, as context, or with a question), is the
|
|
18
|
+
[claude-peer bundle](plugins.md#the-claude-peer-bundle): Claude pairs with the
|
|
19
|
+
chi session, chi's `claude_send` tool or `/claude` writes to Claude's inbox,
|
|
20
|
+
and Claude's answer comes back as attached context.
|
|
21
|
+
|
|
16
22
|
## The commands
|
|
17
23
|
|
|
18
24
|
```sh
|
|
19
25
|
chi send --new --wait --format json [--dir D] [--model M] [--timeout S] -m "task"
|
|
20
26
|
chi send --wait --format json [--timeout S] [-m "follow-up"] ID
|
|
27
|
+
chi send --queue --wait --format json [--timeout S] -m "follow-up" ID
|
|
21
28
|
chi answer ID --question QID (--option N|LABEL)... [--text T] [--timeout S] [--format json]
|
|
22
29
|
chi answer ID --question QID --dismiss [--timeout S] [--format json]
|
|
23
30
|
```
|
|
24
31
|
|
|
25
32
|
`chi answer` waits after answering, the same way `chi send --wait` does, so the
|
|
26
|
-
next question comes back as exit 3 again.
|
|
33
|
+
next question comes back as exit 3 again. What it waits for is the answered
|
|
34
|
+
turn: the turn that asked (or, for the step-limit question, the turn it
|
|
35
|
+
continues), so its reply comes back, not that of a turn queued behind it.
|
|
36
|
+
A worker older than this (its `bridge.json` doesn't name `turn_ids`) gets
|
|
37
|
+
the session's next reply instead. `--option` is 1-based or the label,
|
|
27
38
|
repeated on a multi-select question. `--text` is free text (where the question
|
|
28
39
|
allows it) or the reason for a Deny. `--dismiss` leaves the question
|
|
29
40
|
unanswered: the model doesn't do what it asked about and finishes its reply.
|
|
@@ -37,7 +48,7 @@ Exit codes, both commands:
|
|
|
37
48
|
| 0 | `not_continued` | (`chi answer`) a Stop answered the step-limit question; the turn's work so far stays |
|
|
38
49
|
| 3 | `question` | chi waits for an answer: `question`, `answer_with` |
|
|
39
50
|
| 4 | `running` | `--timeout` passed; the turn goes on, wait again |
|
|
40
|
-
| 1 | `failed`, `canceled`, `limit`, `no_answer`, `error`, `worker_gone`, `stopped` | `detail` says what happened (`limit`: the turn ran out of steps and nobody can be asked, e.g. an older worker; `chi send ID -m '/continue yes'` continues it). `failed` adds `error_kind` (the provider error's kind: `credits`, `credits_held`, `rate_limited`, `server`, `auth`, `connection`, …) and `retryable` when known; `canceled` adds `cancel_reason` (`user`: a Stop in the web
|
|
51
|
+
| 1 | `failed`, `canceled`, `limit`, `no_answer`, `error`, `worker_gone`, `stopped` | `detail` says what happened (`limit`: the turn ran out of steps and nobody can be asked, e.g. an older worker; `chi send ID -m '/continue yes'` continues it). `failed` adds `error_kind` (the provider error's kind: `credits`, `credits_held`, `rate_limited`, `server`, `auth`, `connection`, …) and `retryable` when known; `canceled` adds `cancel_reason` (`user`: a Stop in the web; `session_stopped`: a session stop, `chi sessions stop` or the web's; `hook`; `ctrl_c`; `manual`) and `stopped_by` (the hook that stopped it, e.g. `loop-guard`) when known |
|
|
41
52
|
| 2 | (no JSON) | usage error, or an option the question doesn't offer |
|
|
42
53
|
| 130 | `running` | Ctrl-C; the turn goes on |
|
|
43
54
|
|
|
@@ -133,8 +144,18 @@ answers from any local process.
|
|
|
133
144
|
isn't sent: it exits 1 with the `chi answer` command for the question. The
|
|
134
145
|
step-limit question is the exception: it waits between turns, so a message
|
|
135
146
|
goes in, drops it and starts a new turn.
|
|
147
|
+
- A follow-up to a session whose turn still runs (`busy: a turn is running`,
|
|
148
|
+
exit 1, say you waited on with `--timeout` and got exit 4) goes in with
|
|
149
|
+
`chi send --queue --wait -m "…" ID`: it runs as a turn of its own after the
|
|
150
|
+
running one, and the wait is for that turn's answer alone, never the running
|
|
151
|
+
turn's (`--format json` adds `"delivery": "queue"`). A question the running
|
|
152
|
+
turn asks meanwhile isn't yours: the wait goes on through it (until your
|
|
153
|
+
`--timeout`); one the queued turn asks comes back as exit 3. `chi answer`
|
|
154
|
+
on the running turn's question gets that turn's reply, not your queued one.
|
|
136
155
|
- `chi send --wait ID` with no message waits past a question already reported
|
|
137
|
-
(the step-limit question too), until someone answers it.
|
|
156
|
+
(the step-limit question too), until someone answers it. It waits for the
|
|
157
|
+
turn running when it starts (or the one the step-limit question continues),
|
|
158
|
+
and prints that turn's reply, not a queued one's after it.
|
|
138
159
|
- An answer to a question that is no longer open (you answered it in the web
|
|
139
160
|
first) isn't sent; `chi answer` says so and waits for what comes next. Its
|
|
140
161
|
JSON then has `"answered_here": false` (absent when the answer went in), so
|
|
@@ -181,6 +202,8 @@ Start a task: `chi send --new --wait --format json --timeout 500 -m "<task>"`
|
|
|
181
202
|
Run each chi command on its own, nothing chained after it: the exit code comes
|
|
182
203
|
back with the output. Talk to chi only through these commands; never call its
|
|
183
204
|
Bridge or web API directly. Follow-ups in the same session:
|
|
184
|
-
`chi send --wait --format json --timeout 500 -m "…" <session_id
|
|
205
|
+
`chi send --wait --format json --timeout 500 -m "…" <session_id>`; if that says
|
|
206
|
+
`busy` (a turn still runs), add `--queue`: the follow-up runs after that turn
|
|
207
|
+
and you get its own answer.
|
|
185
208
|
Keep `--timeout` under your shell tool's limit (Claude Code: 600 s per call).
|
|
186
209
|
```
|
data/docs/testing.md
CHANGED
|
@@ -15,15 +15,24 @@ bug) or a broken spec: fix it, or disable that line with the reason (`# rubocop:
|
|
|
15
15
|
No spec reads your own setup. The suite points `XDG_CONFIG_HOME` and `XDG_STATE_HOME` at temp folders,
|
|
16
16
|
clears your `SAMAGOTCHI_*` environment, and keeps every spec off the network (WebMock).
|
|
17
17
|
|
|
18
|
-
The web specs run in plain node. One that loads lit-html (`turn_view.js`,
|
|
19
|
-
`
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
The web specs run in plain node. One that loads lit-html, itself or through a module it imports (`turn_view.js`,
|
|
19
|
+
`turn_html.js`, `card.js`, `copy.js`, `images.js`, `scope.js`, `command_view.js`, `diff_view.js`, `read_view.js`,
|
|
20
|
+
`question_cards.js`, `stream_handlers.js`, the chips, `info_bar.js`, `lists_view.js`, `context_chips.js`, `families_view.js`,
|
|
21
|
+
`chains.js`, `vendor/lit-html.js`) needs a DOM: `import "./dom_shim.js";` as its first import (happy-dom, a devDependency only,
|
|
22
|
+
never shipped; about 0.3 s per spec file that loads it; lit reads `document` as it loads). The shim's `markupOf(template)`,
|
|
23
|
+
`htmlOf(nodes)` and `canon(html)` compare markup without lit's marker comments; the DOM writes text with only `&`, `<`, `>`
|
|
24
|
+
escaped (a `"` in text stays `"`) and an attribute with `&` and `"` escaped. happy-dom 20 picks the wrong option of a `<select>` whose options lit inserted with
|
|
22
25
|
`?selected`: read the option marked `selected` (`option[selected]`), not `select.value`; the browser follows the mark.
|
|
23
26
|
A select bound with `.value` instead reads right in both, but lit sets it before the options exist on a first render:
|
|
24
27
|
`llm_context_chip.js`'s `renderForm` draws twice then. Assert a kept node with `assert.ok(a === b)`: a failing
|
|
25
28
|
`assert.equal` on two DOM nodes inspects the whole DOM (huge output, can stall the run).
|
|
26
29
|
|
|
30
|
+
Where a composer line goes is decided twice, in Ruby (`ComposerRoute`, both terminals) and in the web
|
|
31
|
+
(`composer.js#routeComposerLine`). `spec/fixtures/composer_routes.json` holds the cases both answer: rspec
|
|
32
|
+
(`spec/composer_route_spec.rb`) and `npm test` (`spec/web/public/composer.test.js`) run every row. A change to
|
|
33
|
+
either router adds its row there; a row where a UI differs on purpose (or a found divergence) carries a `web` or
|
|
34
|
+
`repl` override and a `note`.
|
|
35
|
+
|
|
27
36
|
## While you work: affected specs
|
|
28
37
|
|
|
29
38
|
```sh
|
|
@@ -112,6 +112,10 @@ module Samagotchi
|
|
|
112
112
|
# wakes, and it must not come back as exit 3.
|
|
113
113
|
cursor = ReplyWait.newest_reply(id, state_dir: @state_dir)
|
|
114
114
|
baseline = ReplyWait.baseline_of(session, question_id: qid)
|
|
115
|
+
# The turn the answer is about: the one asking (running), or the one
|
|
116
|
+
# whose continue offer stands. Its own reply is the answer's, not a
|
|
117
|
+
# turn queued behind it; nil (an older worker): the next reply.
|
|
118
|
+
turn = ReplyWait.own_turn(session, session_dir: Session.session_dir(id, state_dir: @state_dir))
|
|
115
119
|
|
|
116
120
|
if pending && pending[:id].to_s == qid
|
|
117
121
|
return failure(not_dismissable(pending)) if options[:dismiss] && ParentReport.continue?(pending)
|
|
@@ -129,7 +133,7 @@ module Samagotchi
|
|
|
129
133
|
end
|
|
130
134
|
# Answered here, or no longer open (answered elsewhere, or a newer
|
|
131
135
|
# question): what comes next either way.
|
|
132
|
-
wait_for_reply(id, cursor: cursor, baseline: baseline, timeout: options[:timeout])
|
|
136
|
+
wait_for_reply(id, cursor: cursor, baseline: baseline, timeout: options[:timeout], turn: turn)
|
|
133
137
|
end
|
|
134
138
|
|
|
135
139
|
# The labels to send. A number is an option's place (1-based); an
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
require_relative "log"
|
|
5
5
|
require_relative "ref"
|
|
6
|
+
require_relative "row_refs"
|
|
6
7
|
|
|
7
8
|
module Samagotchi
|
|
8
9
|
# One ref on the answer: the text it stands for (+match+, what the page
|
|
@@ -27,30 +28,20 @@ module Samagotchi
|
|
|
27
28
|
MAX_MATCH = 500
|
|
28
29
|
|
|
29
30
|
# A hook's {match:, kind:, target:, label:, actions:}, or a stored
|
|
30
|
-
# entry (String or Symbol keys, nested ones too). A file ref
|
|
31
|
-
#
|
|
32
|
-
#
|
|
31
|
+
# entry (String or Symbol keys, nested ones too). A file ref is held to
|
|
32
|
+
# the session's folder as a plugin row's is (RowRefs.inside, plan D7):
|
|
33
|
+
# +roots+ is RowRefs.roots of it; a relative path is resolved against
|
|
34
|
+
# it (+relative+; a stored one was stored absolute, so it is dropped),
|
|
35
|
+
# a path outside it refused, and the path kept under its real
|
|
36
|
+
# spelling. With no folder (no +roots+), an absolute path stays.
|
|
33
37
|
# @raise [ArgumentError] anything invalid
|
|
34
|
-
def self.from(value,
|
|
38
|
+
def self.from(value, roots: [], relative: true)
|
|
35
39
|
raise ArgumentError, "a ref is a Hash {match:, kind:, target:, label:, actions:}" unless value.is_a?(Hash)
|
|
36
40
|
|
|
37
41
|
value = value.transform_keys(&:to_sym)
|
|
38
42
|
ref = Ref.new(kind: value[:kind], target: value[:target], label: value[:label], actions: value[:actions] || [])
|
|
39
|
-
new(match: value[:match], ref:
|
|
43
|
+
new(match: value[:match], ref: RowRefs.inside(ref, roots, relative))
|
|
40
44
|
end
|
|
41
|
-
|
|
42
|
-
def self.absolute(ref, cwd)
|
|
43
|
-
return ref unless ref.kind == Ref::FILE
|
|
44
|
-
|
|
45
|
-
path = ref.target[:path]
|
|
46
|
-
return ref if File.absolute_path?(path)
|
|
47
|
-
raise ArgumentError, "relative path, no session folder" if cwd.to_s.empty?
|
|
48
|
-
|
|
49
|
-
# absolute_path, not expand_path: a leading ~ is a name, never a home.
|
|
50
|
-
Ref.new(kind: ref.kind, target: ref.target.merge(path: File.absolute_path(path, cwd.to_s)), label: ref.label,
|
|
51
|
-
actions: ref.actions)
|
|
52
|
-
end
|
|
53
|
-
private_class_method :absolute
|
|
54
45
|
end
|
|
55
46
|
|
|
56
47
|
# The refs after_turn hooks attach to a turn's answer (event[:refs]),
|
|
@@ -71,12 +62,13 @@ module Samagotchi
|
|
|
71
62
|
# Stored refs (+raw+, as read from session.json or a snapshot), each
|
|
72
63
|
# rebuilt and checked again, the caps applied; what isn't valid any
|
|
73
64
|
# more is dropped. Never raises: a reload must show the conversation.
|
|
74
|
-
# A relative file path is dropped (
|
|
65
|
+
# A relative file path is dropped (it was stored absolute), and with
|
|
66
|
+
# +cwd+ (the session's folder) a file outside it.
|
|
75
67
|
# @return [Array<Hash>] the refs to send ({match:, kind:, target:, …})
|
|
76
|
-
def self.for_display(raw)
|
|
68
|
+
def self.for_display(raw, cwd: nil)
|
|
77
69
|
return [] if raw.nil?
|
|
78
70
|
|
|
79
|
-
kept = new([{ role: "model", KEY => raw }], cwd:
|
|
71
|
+
kept = new([{ role: "model", KEY => raw }], cwd: cwd).to_a
|
|
80
72
|
dropped = raw.is_a?(Array) ? raw.length - kept.length : 1
|
|
81
73
|
Log.debug(:web, "display_refs_dropped", count: dropped) if dropped.positive?
|
|
82
74
|
kept
|
|
@@ -92,7 +84,7 @@ module Samagotchi
|
|
|
92
84
|
def initialize(messages, cwd:)
|
|
93
85
|
last = Array(messages).last
|
|
94
86
|
@target = last if last.is_a?(Hash) && (last[:role] || last["role"]).to_s == "model"
|
|
95
|
-
@
|
|
87
|
+
@roots = RowRefs.roots(cwd)
|
|
96
88
|
@refs = []
|
|
97
89
|
@seen = {}
|
|
98
90
|
@bytes = 2 # the list's []
|
|
@@ -149,7 +141,7 @@ module Samagotchi
|
|
|
149
141
|
unknown = value.keys - KEYS
|
|
150
142
|
return reject(hook, "has unknown keys #{unknown.map(&:to_s).inspect}") unless unknown.empty?
|
|
151
143
|
|
|
152
|
-
entry = AnswerRef.from(value,
|
|
144
|
+
entry = AnswerRef.from(value, roots: @roots).to_h
|
|
153
145
|
return true if @seen.key?(entry)
|
|
154
146
|
return over_cap(hook, "#{MAX_REFS} refs") if @refs.length >= MAX_REFS
|
|
155
147
|
|
|
@@ -163,11 +155,11 @@ module Samagotchi
|
|
|
163
155
|
end
|
|
164
156
|
|
|
165
157
|
# A stored entry back in the list, checked again (a relative path is
|
|
166
|
-
# dropped: it was stored absolute
|
|
167
|
-
# caps go quietly.
|
|
158
|
+
# dropped: it was stored absolute; so is a file outside the folder).
|
|
159
|
+
# Duplicates and what is over the caps go quietly.
|
|
168
160
|
# @return [Boolean] kept
|
|
169
161
|
def keep(raw)
|
|
170
|
-
entry = AnswerRef.from(raw,
|
|
162
|
+
entry = AnswerRef.from(raw, roots: @roots, relative: false).to_h
|
|
171
163
|
return false if @seen.key?(entry) || @refs.length >= MAX_REFS
|
|
172
164
|
|
|
173
165
|
size = JSON.generate(entry).bytesize + (@refs.empty? ? 0 : 1)
|
|
@@ -226,6 +226,7 @@ module Samagotchi
|
|
|
226
226
|
when :turn_enqueued
|
|
227
227
|
queued = { enqueued_id: event[:enqueued_id], client_id: event[:client_id], prompt: event[:prompt] }
|
|
228
228
|
queued[:images] = event[:images] if event[:images]
|
|
229
|
+
queued[:delivery] = event[:delivery] if event[:delivery]
|
|
229
230
|
@queued << queued
|
|
230
231
|
when :recap_ready
|
|
231
232
|
# One collected just as a turn started describes the chat before it.
|
data/lib/samagotchi/bridge.rb
CHANGED
|
@@ -29,6 +29,7 @@ require_relative "version"
|
|
|
29
29
|
require_relative "worker_sidecar"
|
|
30
30
|
require_relative "relay_verifier"
|
|
31
31
|
require_relative "relay_watcher"
|
|
32
|
+
require_relative "reply_wait"
|
|
32
33
|
require_relative "tools/task_runtime"
|
|
33
34
|
|
|
34
35
|
module Samagotchi
|
|
@@ -52,8 +53,9 @@ module Samagotchi
|
|
|
52
53
|
DEFAULT_RING_CAPACITY = 256
|
|
53
54
|
DEFAULT_HEARTBEAT_INTERVAL = 15.0
|
|
54
55
|
# Cancel reasons a client may name (the web sends user, an attached TUI
|
|
55
|
-
# ctrl_c
|
|
56
|
-
|
|
56
|
+
# ctrl_c, a session stop session_stopped: SessionManager.stop_session);
|
|
57
|
+
# anything else is :manual, so client input never mints symbols.
|
|
58
|
+
CANCEL_REASONS = %w[manual user ctrl_c session_stopped].freeze
|
|
57
59
|
# How long #stop waits for requests it is answering (not open streams).
|
|
58
60
|
REQUEST_GRACE_SECONDS = 1.0
|
|
59
61
|
# How long #stop waits for each killed RelayWatcher thread to end.
|
|
@@ -180,6 +182,25 @@ module Samagotchi
|
|
|
180
182
|
raise e
|
|
181
183
|
end
|
|
182
184
|
|
|
185
|
+
# Raised on a connection thread whose request would queue something
|
|
186
|
+
# (a turn, a command, an exit) after #close_intake: the connection
|
|
187
|
+
# closes unanswered, as #stop's kill of a request still waiting did.
|
|
188
|
+
class IntakeClosed < StandardError; end
|
|
189
|
+
|
|
190
|
+
# With the event log held (the worker leaving: Worker#leave_idle,
|
|
191
|
+
# #leave_on_request): no connection is accepted and no request queues
|
|
192
|
+
# a turn, a command or an exit from here on. #stop, called once the log
|
|
193
|
+
# is let go, does the rest: a request that waited on the log then ends
|
|
194
|
+
# at once instead of holding #stop for its REQUEST_GRACE_SECONDS.
|
|
195
|
+
def close_intake
|
|
196
|
+
@mutex.synchronize { @stopped = true }
|
|
197
|
+
begin
|
|
198
|
+
@server&.close
|
|
199
|
+
rescue StandardError
|
|
200
|
+
nil
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
183
204
|
# Stop the acceptor, release the socket and remove the sidecar, so clients
|
|
184
205
|
# don't have to find it stale. Does not stop the owning turn. Joins the
|
|
185
206
|
# acceptor thread so the process can exit cleanly (Ruby waits for a thread
|
|
@@ -394,7 +415,7 @@ module Samagotchi
|
|
|
394
415
|
log_request(method, request[:path], started)
|
|
395
416
|
break if close_after_request?(headers)
|
|
396
417
|
end
|
|
397
|
-
rescue Errno::EPIPE, Errno::ECONNRESET, IOError
|
|
418
|
+
rescue Errno::EPIPE, Errno::ECONNRESET, IOError, IntakeClosed
|
|
398
419
|
nil
|
|
399
420
|
rescue StandardError => e
|
|
400
421
|
# The thread doesn't report (report_on_exception = false): log it.
|
|
@@ -476,7 +497,11 @@ module Samagotchi
|
|
|
476
497
|
# task_stop: POST tasks/stop (#handle_task_stop), the web's stop-task button.
|
|
477
498
|
# reparent: the worker reads Session::PARENT_FILE (its rings, each turn's
|
|
478
499
|
# start), so a continue may move it to the chain's new link (ChildMove).
|
|
479
|
-
|
|
500
|
+
# delivery: POST /turn runs every Delivery value; a queue waits for the
|
|
501
|
+
# turn's end (Delivery::QUEUE_FEATURE). A worker without it would steer.
|
|
502
|
+
# turn_ids: the running turn's checkpoint mark, a continue turn and a
|
|
503
|
+
# Stop's last_turn carry the turn's enqueued_id (ReplyWait.own_turn).
|
|
504
|
+
FEATURES = ["restart", "task_stop", "reparent", Delivery::QUEUE_FEATURE, ReplyWait::TURN_IDS_FEATURE].freeze
|
|
480
505
|
|
|
481
506
|
# The routes, by method and what follows /session/:id/: a handler takes
|
|
482
507
|
# the session id and the request body and returns [headers, status, body].
|
|
@@ -728,6 +753,7 @@ module Samagotchi
|
|
|
728
753
|
# A card's action: the UIs leave its line out (the card is the echo).
|
|
729
754
|
command = QueuedCommand.mint(line, client_id: client_id, card: card)
|
|
730
755
|
queued = @engine.synchronize_events do
|
|
756
|
+
raise IntakeClosed if stopped?
|
|
731
757
|
next false if expired?("command_expired", deadline, sid: session_id, client_id: command.client_id)
|
|
732
758
|
|
|
733
759
|
queue_command_locked(command)
|
|
@@ -777,6 +803,7 @@ module Samagotchi
|
|
|
777
803
|
return BAD_DEADLINE unless deadline_valid?(deadline)
|
|
778
804
|
|
|
779
805
|
reason = @engine.synchronize_events do
|
|
806
|
+
raise IntakeClosed if stopped?
|
|
780
807
|
next :expired if expired?("exit_expired", deadline, sid: session_id, client_id: client_id)
|
|
781
808
|
|
|
782
809
|
@on_exit_request.call(client_id, **options)
|
|
@@ -847,18 +874,25 @@ module Samagotchi
|
|
|
847
874
|
enqueued_id = SecureRandom.uuid
|
|
848
875
|
# Before it is queued: a drain after this may have taken it (#cut_for_steer).
|
|
849
876
|
epoch = @engine.input_epoch
|
|
877
|
+
# Whether a queue waits for a running turn: read with the write under
|
|
878
|
+
# the event log, so no :turn_started or :turn_completed falls between.
|
|
879
|
+
# To an idle worker the queued file simply runs as the next turn.
|
|
880
|
+
queued_behind_turn = false
|
|
850
881
|
# Write and announce with the event log held: the worker can't emit
|
|
851
882
|
# this turn's :turn_started (or merge it mid-turn) before
|
|
852
883
|
# :turn_enqueued, and a failed write announces nothing.
|
|
853
884
|
enqueued = @engine.synchronize_events do
|
|
885
|
+
raise IntakeClosed if stopped?
|
|
854
886
|
next :expired if expired?("turn_expired", deadline, sid: sid, client_id: client_id)
|
|
855
887
|
|
|
888
|
+
queued_behind_turn = Delivery.queue?(delivery) && !@accumulator.current_turn.nil?
|
|
856
889
|
enqueue_turn(prompt: prompt, client_id: client_id, enqueued_id: enqueued_id,
|
|
857
890
|
no_interrupt: no_interrupt, images: images, delivery: delivery).tap do |ok|
|
|
858
891
|
next unless ok
|
|
859
892
|
|
|
860
893
|
enqueued_event = { type: :turn_enqueued, enqueued_id: enqueued_id, client_id: client_id, prompt: prompt.to_s }
|
|
861
894
|
enqueued_event[:images] = images unless images.empty?
|
|
895
|
+
enqueued_event[:delivery] = Delivery::QUEUE if queued_behind_turn
|
|
862
896
|
@engine.announce(enqueued_event)
|
|
863
897
|
@on_input&.call
|
|
864
898
|
end
|
|
@@ -874,7 +908,9 @@ module Samagotchi
|
|
|
874
908
|
# the running turn's next step.
|
|
875
909
|
cut = nil
|
|
876
910
|
cut = @engine.cut_for_steer(Steer.source_for_client(client_id), epoch: epoch) if images.empty? && Delivery.cut?(delivery)
|
|
877
|
-
|
|
911
|
+
# A queue to an idle worker is a turn now: the ack says next_step.
|
|
912
|
+
ack_delivery = Delivery.queue?(delivery) && !queued_behind_turn ? Delivery::NEXT_STEP : delivery
|
|
913
|
+
ack = { status: "accepted", enqueued_id: enqueued_id, session_id: @session_id, delivery: ack_delivery }
|
|
878
914
|
ack[:cut] = cut.to_s if cut
|
|
879
915
|
ack[:cut_after] = @engine.steer_cut_after if cut == :waits
|
|
880
916
|
[{}, 202, ack]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: claude-peer
|
|
3
|
+
version: 0.1.0
|
|
4
|
+
scope: system
|
|
5
|
+
description: "Pair this session with a Claude Code session you work with: the claude_send tool and /claude send it a part of the work (a plan step, a diff, a finding) to validate or as context, and Claude's answer comes back as the claude-review context; /claude install-skill installs Claude's chi-peer skill, which pairs"
|
|
6
|
+
trust_level: reviewed
|
|
7
|
+
files:
|
|
8
|
+
skill_claude_peer.md:
|
|
9
|
+
sha256: sha256:dff0175d0370f43920daf5ecbb81315fdd2c7d8063a33fbefc75c7d0e7312993
|
|
10
|
+
description: "Send a part of the work to the paired Claude Code session (claude_send): only when the user asks; validate, context or question; Claude's answer comes as the claude-review context"
|
|
11
|
+
plugin:
|
|
12
|
+
file: plugin.rb
|
|
13
|
+
sha256: sha256:bf5a95eb125ba4831466e177295bdaba1f625e255b027cea1e59992310d8300e
|
|
14
|
+
scripts:
|
|
15
|
+
pair: sha256:033f1fc6a644950b57e71865253526a0a487bb4c9fd5a61bb89d031b65f40417
|
|
16
|
+
chi-peer-SKILL.md: sha256:f35f726b1a86091ff69d289e8dd58794ac6f44879a63630fcc88d69867c56e20
|
|
17
|
+
# Two things need chi 0.63.0, and the bundle works without them on 0.62:
|
|
18
|
+
# ctx.context.list's summary: (the /claude card's last answer; without it
|
|
19
|
+
# the card shows no summary), and a pushed source's first wake: true push
|
|
20
|
+
# waking the session (on 0.62 Claude's first answer arrives as a note
|
|
21
|
+
# without waking; later ones wake).
|
|
22
|
+
requires_chi: ">= 0.62.0"
|