samagotchi 0.188.0 → 0.189.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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -1
  3. data/docs/architecture.md +5 -3
  4. data/docs/bundle-authoring.md +1 -1
  5. data/docs/bundles.md +15 -0
  6. data/docs/cli.md +23 -5
  7. data/docs/guardrails.md +3 -2
  8. data/docs/internals/bundles.md +3 -3
  9. data/docs/internals/pages.md +65 -18
  10. data/docs/internals/session-lifecycle.md +19 -6
  11. data/docs/internals/web-frontend.md +3 -3
  12. data/docs/internals/web-push.md +11 -5
  13. data/docs/pages.md +68 -15
  14. data/docs/peers.md +3 -0
  15. data/docs/plugins.md +25 -1
  16. data/docs/releasing.md +1 -1
  17. data/docs/scheduling.md +71 -8
  18. data/docs/sessions.md +4 -4
  19. data/docs/testing.md +1 -1
  20. data/lib/samagotchi/bundle_manifest.rb +34 -5
  21. data/lib/samagotchi/bundles/schedules/manifest.yml +11 -0
  22. data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +159 -0
  23. data/lib/samagotchi/bundles/system/manifest.yml +3 -3
  24. data/lib/samagotchi/bundles/system/self_map.md +2 -2
  25. data/lib/samagotchi/bundles/system/skill_pages.md +9 -4
  26. data/lib/samagotchi/installed_bundles/manifest.rb +8 -1
  27. data/lib/samagotchi/memory_bundle/asset_installer.rb +3 -2
  28. data/lib/samagotchi/memory_bundle/builder.rb +1 -0
  29. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  30. data/lib/samagotchi/page_command.rb +9 -6
  31. data/lib/samagotchi/pages/bundled.rb +83 -0
  32. data/lib/samagotchi/pages/launch.rb +14 -2
  33. data/lib/samagotchi/pages/runner.rb +7 -2
  34. data/lib/samagotchi/pages/runtime.rb +83 -0
  35. data/lib/samagotchi/pages/store.rb +18 -5
  36. data/lib/samagotchi/pages.rb +47 -3
  37. data/lib/samagotchi/peer_continue.rb +14 -8
  38. data/lib/samagotchi/run_options.rb +1 -1
  39. data/lib/samagotchi/schedule_command.rb +79 -37
  40. data/lib/samagotchi/schedule_display.rb +69 -0
  41. data/lib/samagotchi/schedules/busy_part.rb +146 -0
  42. data/lib/samagotchi/schedules/edit_part.rb +120 -0
  43. data/lib/samagotchi/schedules/history_part.rb +51 -0
  44. data/lib/samagotchi/schedules/listing_part.rb +52 -0
  45. data/lib/samagotchi/schedules/notices_part.rb +116 -0
  46. data/lib/samagotchi/schedules/protection_part.rb +103 -0
  47. data/lib/samagotchi/schedules/recurrence.rb +103 -0
  48. data/lib/samagotchi/schedules.rb +134 -330
  49. data/lib/samagotchi/session_access/local.rb +9 -1
  50. data/lib/samagotchi/session_access/remote.rb +2 -1
  51. data/lib/samagotchi/session_chain/delete.rb +7 -5
  52. data/lib/samagotchi/session_lifecycle/allowed.rb +2 -1
  53. data/lib/samagotchi/session_lifecycle.rb +3 -2
  54. data/lib/samagotchi/session_manager/removal.rb +16 -9
  55. data/lib/samagotchi/session_protection.rb +99 -0
  56. data/lib/samagotchi/session_retention.rb +21 -6
  57. data/lib/samagotchi/sessions_command.rb +4 -0
  58. data/lib/samagotchi/testing/boundary.rb +13 -0
  59. data/lib/samagotchi/tools/cancel_schedule.rb +5 -2
  60. data/lib/samagotchi/turn_outcome.rb +41 -0
  61. data/lib/samagotchi/version.rb +1 -1
  62. data/lib/samagotchi/web/page_routes.rb +13 -8
  63. data/lib/samagotchi/web/public/app.js +12 -18
  64. data/lib/samagotchi/web/public/index.html +11 -2
  65. data/lib/samagotchi/web/public/owner_claim.js +7 -5
  66. data/lib/samagotchi/web/public/page_links.js +1 -1
  67. data/lib/samagotchi/web/public/page_view.js +8 -5
  68. data/lib/samagotchi/web/public/pages.css +9 -2
  69. data/lib/samagotchi/web/public/pages_group.js +21 -19
  70. data/lib/samagotchi/web/public/pages_list.js +73 -0
  71. data/lib/samagotchi/web/public/pages_model.js +54 -0
  72. data/lib/samagotchi/web/public/pages_wiring.js +66 -0
  73. data/lib/samagotchi/web/public/views.js +77 -23
  74. data/lib/samagotchi/web/push/notifier.rb +4 -3
  75. data/lib/samagotchi/web/server.rb +3 -1
  76. data/lib/samagotchi/web/session_hub.rb +64 -19
  77. metadata +17 -1
@@ -186,16 +186,22 @@ from saying it twice across restarts). The key's `chi:` prefix keeps sw.js from
186
186
  | `updated` | `chi:updated:<X>` | "chi updated to X" / "chi web and 3 sessions restarted; 1 restarts when idle." + the edited files |
187
187
  | `restart_failed` | `chi:restart_failed:<X>` | "chi X is installed; chi web didn't restart" / why (a failed `--version` preflight, no wrapper), "Restart chi web in its terminal." |
188
188
 
189
- **A disabled schedule's show.** `SessionHub#run_schedule_tick` emits one `schedule_disabled` hub event per
190
- `Schedules::Outcome#disabled` of its tick (`#publish_disabled`, under `@monitor` like `publish_notice`): `{id, title,
191
- kind: skipped|failed, count, reason, session_id, at}`, `session_id` being `Schedules#session_of` (the resolved
192
- target, else the head) and `at` the tick's wall time. The page's event stream passes it to the tabs, which have no
189
+ **A disabled schedule's show.** Any ticker (chi web's, a cron `chi schedule tick`, a `run-now`) that turns a
190
+ recurring schedule off by its budget leaves a `disabled_notice` on its entry (`Schedules#skipped` / `#failed`,
191
+ under the rule `#mark_disabled` shares). The hub claims them with `Schedules#claim_disabled_notices` (a lock-free
192
+ read, then a non-blocking lock that clears each notice before it is emitted: at most once, and once across two chi
193
+ webs, since `@updates_seen` is per process) right after its own tick in `#run_schedule_tick` and once per
194
+ `schedule.tick_interval_minutes` from `#schedule_tick`, auto_tick off too, but never from the seeding
195
+ `scan(tick: false)`, which runs before the Notifier subscribes. `#publish_disabled` emits one `schedule_disabled`
196
+ hub event per claimed notice (under `@monitor` like `publish_notice`): `{id, title, kind: skipped|failed, count,
197
+ reason, session_id, at}`, `session_id` being the resolved target at claim time, else the head, and `at` the
198
+ disabling tick's time. A notice older than a day or whose entry is on again is cleared unpushed. The page's event stream passes it to the tabs, which have no
193
199
  listener. The Notifier shows it with reason `schedule`, key and tag `chi:schedule_disabled:<id>:<at>` (`chi:`: not
194
200
  a waiting key; `<at>`: a later disable shows again; the same event twice shows once, `@updates_seen`), title
195
201
  "Schedule disabled: <title or the id's first 8>", body "skipped 12 times: <reason>" + a line `chi schedule enable
196
202
  <short id>`, url `Notifier.session_url` of the session's summary, `/` when it has none. Two exceptions to the rules
197
203
  above: it is pushed with a page in front (`UNSEEN_ON_PAGE`: the page has no schedules view), and it gets
198
- `UPDATE_TTL` (24 h) like an update. A cron tick or `chi schedule run-now` emits nothing (no hub).
204
+ `UPDATE_TTL` (24 h) like an update. With no chi web running, a cron tick's notice waits in `schedules.json`.
199
205
 
200
206
  `updated` is sent by the **restarted** chi web (its AutoUpdate finds the run at `phase: web`): the exec ends the
201
207
  old process, and `Notifier#stop` does not flush the shows still in their hold, so a push queued just before it would
data/docs/pages.md CHANGED
@@ -14,7 +14,7 @@ Say what you want to see and that you want it kept: "make me a page of …", "I'
14
14
  "keep this as a page". chi follows its shipped pages skill (`skill_pages`, [docs/memory.md](memory.md#skills)): it
15
15
  finds the pages folder with `chi self`, writes `NAME.rb` there, runs `chi page run NAME`, compares the stored rows
16
16
  with where they came from, fixes what failed, and answers in a line or two with the link `[Title](#/p/NAME)` (with
17
- Markdown off, the link is plain text: the page is in All sessions, under Pages). "Change the page …" edits the script
17
+ Markdown off, the link is plain text: the page is under the `pages` pill in chi web's top bar). "Change the page …" edits the script
18
18
  and runs it again; "remove the page …" is `chi page rm NAME`.
19
19
 
20
20
  - It uses `chi page list`, the read tool, `chi page run` and `chi page rm`, never a shell command that names the pages
@@ -77,6 +77,50 @@ the script adds them, at most 20.
77
77
  A rule broken fails the run with a message naming the block and the limit: `table 2: column "cost" has format
78
78
  :percent (use :money or :count)`, `table 1 has 13 columns (at most 12)`.
79
79
 
80
+ ## Reading chi's data: `runtime`
81
+
82
+ From chi 0.189.0 a script has **`runtime`** next to `page`: what chi itself knows, read in the page's own process
83
+ from this chi's code (no `chi` on `PATH`, no second boot, nothing changed). A page that never calls it loads none of
84
+ it. On an older chi `runtime` is a `NameError`, and the run fails with it (the last good result kept).
85
+
86
+ - **`runtime.schedules`**: every durable schedule ([scheduling.md](scheduling.md)), the earliest due first. Each has
87
+ the keys `chi schedule list --all --format json` gives a schedule as methods: `id`, `name` (its title, or the start
88
+ of its prompt in quotes), `title`, `mode`, `prompt`, `recurrence_words`, `next_due`, `last_fired`, `last_result`,
89
+ `enabled`, `state` (`on`, `firing` or `off`), `off_reason` (`owner`, `skips`, `failures`, `done`, `missed` or
90
+ `off`; nil unless off), `skips`, `max_skips`, `failures`, `max_failures`, `skip_window`, `anchor`, `head`,
91
+ `created_at`, and `history`: its runs, oldest first, each with `at`, `status`, `detail` and `session_id`. Times are
92
+ ISO 8601 UTC strings, nil when there is none.
93
+ - `runtime.schedules.rows`: every schedule as a table row (all but `history`): `ui.table runtime.schedules.rows,
94
+ columns: { "name" => "Name", "state" => "State" }`.
95
+ - `.warning`: `chi schedule list`'s warning when nothing ticks the schedules, else nil; `.unreadable_ids`: the id of
96
+ each entry of `schedules.json` that doesn't read (nil for one with no id).
97
+ - A `schedules.json` that isn't JSON fails the run with the store's own words (`… is not readable JSON …: fix or
98
+ remove it before changing schedules`). The read takes no lock and writes nothing, though chi's debug log may get a
99
+ line for an entry that doesn't read.
100
+
101
+ `runtime`, like the rest of the page script DSL, is stable for bundles ([plugins.md](plugins.md#stable-for-bundles)).
102
+
103
+ ## Pages from a bundle
104
+
105
+ A bundle can ship pages: its manifest's `pages:` names scripts in its `scripts/` ([plugins.md](plugins.md)), each
106
+ `NAME.rb` the page `NAME`. The shipped **schedules** bundle is one: `chi bundle install schedules` adds a
107
+ **Schedules** page ([scheduling.md](scheduling.md#the-schedules-page)).
108
+
109
+ - A bundle's page is listed and runs as yours does (`chi page list` says `schedules bundle`; its JSON carries
110
+ `bundle` and `script`, the path of the script it runs). It shows in chi web after the next list read (a reload, or
111
+ opening the Pages list): an install isn't watched.
112
+ - **Your own `NAME.rb` replaces it**: with `~/.config/samagotchi/pages/schedules.rb` there, that script runs. That is
113
+ how a bundle's page is made your own: copy its script (`script` in `chi page list --format json`) there and change
114
+ it. Removing your copy brings the bundle's page back, as never ran.
115
+ - **It can't be removed**: `chi page rm schedules` exits 1 with `page schedules comes with the schedules bundle:
116
+ uninstall the bundle or write your own ~/.config/samagotchi/pages/schedules.rb`, and chi web has no Delete for it.
117
+ Uninstalling the bundle takes the page away (its stored result stays on disk, unused).
118
+ - A bundle's page lasts as its installed `manifest.yml` does: one edited since lists no page (chi's debug log says
119
+ `bundle_pages_unread`; `chi bundle status NAME` says the file is edited): reinstall the bundle.
120
+ - A bundle's script runs only as installed: one edited since (or missing) runs nothing, and the run fails with
121
+ `bundle schedules's scripts/schedules.rb differs from the installed one (reinstall the bundle)`.
122
+ - When a chi upgrade changes a bundle's script, the page says "The script changed" until a Refresh.
123
+
80
124
  ## Running
81
125
 
82
126
  - `chi page run NAME` runs the script in a Ruby process of its own and prints a summary of each block: a text's first
@@ -91,21 +135,26 @@ A rule broken fails the run with a message naming the block and the limit: `tabl
91
135
  good result is kept).
92
136
  - One run of a page at a time: a second `chi page run usage` meanwhile exits 1 with `page usage is already running`.
93
137
  - `chi page list` shows each page's title and `ran 2 min ago`, `failed 1 h ago (line 9)` or `(never ran)`, with
94
- `changed` when the script changed since its result was made (the result came from an older script; nothing ran). `chi page rm NAME` removes the script and its result.
138
+ `changed` when the script changed since its result was made (the result came from an older script; nothing ran),
139
+ and the bundle a page comes with. `chi page rm NAME` removes the script and its result (a bundle's page can't be
140
+ removed: [Pages from a bundle](#pages-from-a-bundle)).
95
141
 
96
142
  Exit codes: 0 ran, 1 failed (or no such page, or already running), 2 the command line itself (an unknown flag, a name
97
143
  that breaks the rule). See [the CLI](cli.md#commands).
98
144
 
99
145
  ## In chi web
100
146
 
101
- - **Finding a page.** All sessions has a **Pages** group at its top, in every project scope (pages are global): one row
102
- of cards that scrolls sideways, each with the page's title and `ran 2 min ago`, `failed 1 h ago` or `never ran`. The
103
- search field filters it too, by name and title. With no page (or none the search matches) there is no group. A
104
- `#/p/usage` link in chi's answer opens the page in the same tab; a reload or a copied `…/#/p/usage` link opens it
105
- too.
106
- - **The page** shows in place of the session area: Back (to where it was opened from, else All sessions), the title,
147
+ - **Finding a page.** The top bar has a **`pages`** pill as soon as there is a page, in every view and project scope
148
+ (pages are global), on a desktop and a phone alike; with no page it isn't there. It opens the **Pages list**
149
+ (`#/pages`), in place of the session area: a card per page, each with the page's title and `ran 2 min ago`,
150
+ `failed 1 h ago` or `never ran`; a tap opens the page. The pill appears without a reload when chi makes the first
151
+ page (chi web watches the pages folder), and goes when the last one is removed. A `#/p/usage` link in chi's answer
152
+ opens the page in the same tab; a reload or a copied `…/#/p/usage` link opens it too.
153
+ - **The page** shows in place of the session area: Back (to where it was opened from: "Pages" from the list, "Back"
154
+ from an answer's link; with nowhere to go back to, the Pages list), the title,
107
155
  when it ran and how long it took, **Refresh** and `⋯` with Delete (it asks once, then removes the script and its
108
- result). Opening it shows the stored result at once and runs nothing.
156
+ result, then shows the Pages list; a bundle's page has no `⋯`). Opening it shows the stored result at once and runs
157
+ nothing.
109
158
  - **Refresh** runs the script as `chi page run` does and redraws when the run ends; the button reads "Running…" and
110
159
  can't be pressed twice. A run already going (from a terminal, another tab) says "already running". Stopping chi web
111
160
  stops a Refresh. A Refresh holds its request until the run ends, up to about 32 s (the 30 s cut and its grace): behind
@@ -119,12 +168,15 @@ that breaks the rule). See [the CLI](cli.md#commands).
119
168
  a tap opens the rest as label and value lines; rows keep the script's order there.
120
169
  - **A walk** is drawn by the same widget as in an answer; its lanes scroll sideways inside it.
121
170
  - **Markdown off** (`web.markdown`, the default): a text block shows as plain text, and a link in chi's answer is plain
122
- text too, so the Pages group is the way in. A fenced block inside a text block (a `chi-ui` or a mermaid one) stays
171
+ text too, so the `pages` pill is the way in. A fenced block inside a text block (a `chi-ui` or a mermaid one) stays
123
172
  source on a page.
124
- - **Not live**: the group is read when All sessions opens, a page when it opens and after its own Refresh or Delete.
125
- A run from a terminal shows after the next open or a reload.
126
- - Pages are the owner's: a participant ([chi share](cli.md)) sees no Pages group, no page, and no page route answers
127
- their link.
173
+ - **What is live**: the list (and with it the pill) is read when chi web opens, when the Pages list, All sessions or a
174
+ page opens, after a Delete, and when a page script is made or removed (a script that writes a file into the pages
175
+ folder, which is its working folder, counts too: each of its runs refreshes the open lists). A page itself is read when it opens and
176
+ after its own Refresh; a run from a terminal shows after the next open or a reload (a first run's "ran" time on the
177
+ list too).
178
+ - Pages are the owner's: a participant ([chi share](cli.md)) sees no `pages` pill, no Pages list, no page, and no page
179
+ route answers their link.
128
180
 
129
181
  ## Where a run happens
130
182
 
@@ -145,7 +197,8 @@ that breaks the rule). See [the CLI](cli.md#commands).
145
197
 
146
198
  ## Where things are stored
147
199
 
148
- - The script: `~/.config/samagotchi/pages/NAME.rb`.
200
+ - The script: `~/.config/samagotchi/pages/NAME.rb`; a bundle's in the bundle's own `scripts/` folder under the system
201
+ memories (`.bundles/NAME/scripts/`).
149
202
  - The result: `~/.local/state/samagotchi/pages/NAME/output.json` (`$XDG_STATE_HOME`): the title, the blocks of the last
150
203
  good run, when it ran, how long it took, the sha256 of the script that made it, and the last failure since, if any.
151
204
  Its shape is in [internals/pages.md](internals/pages.md).
data/docs/peers.md CHANGED
@@ -93,6 +93,9 @@ run it. `peer.turns: 0` means a peer's message never wakes chi. `context.wake` i
93
93
  (`serial`, `offset`, `limit`), which pages it.
94
94
  - 200 messages chi hasn't read: further says are refused until it reads them.
95
95
  - The mailbox keeps every message chi hasn't taken, and the newest 50 it has.
96
+ - A continued message is cut at 4 KiB in the new link; run `chi peer ls <previous id>` to read the rest.
97
+ - A `chi peer say` sent at the very moment of a continue may stay with the previous session; run
98
+ `chi peer ls <previous id>` to read it there.
96
99
 
97
100
  ## Continuing a session
98
101
 
data/docs/plugins.md CHANGED
@@ -1737,6 +1737,26 @@ block_renderers:
1737
1737
  side and 100M px. Over that, or if the image doesn't decode, the block stays source. The
1738
1738
  layers and the residual risk are in [internals/block-renderers.md](internals/block-renderers.md).
1739
1739
 
1740
+ ## Pages
1741
+
1742
+ A bundle can ship [pages](pages.md): Ruby scripts the page runner runs, listed under `pages:` (from chi 0.189.0).
1743
+
1744
+ ```yaml
1745
+ # manifest.yml
1746
+ scripts:
1747
+ schedules.rb: sha256:… # scripts/schedules.rb in the bundle
1748
+ pages: [schedules.rb] # scripts: keys named NAME.rb: the page NAME
1749
+ ```
1750
+
1751
+ - Each is a `scripts:` file, so it installs and checks as any script does, and one changed after the install runs
1752
+ nothing until the bundle is reinstalled (a `--dev` install's links aren't checked).
1753
+ - chi web lists them from the install records, running no plugin. The first installed bundle (by name) with a page
1754
+ name has it; a bundle whose `requires_chi` this chi doesn't meet has none.
1755
+ - The owner's own `NAME.rb` in the pages folder replaces a bundle's page, and a bundle's page can't be removed but
1756
+ by uninstalling the bundle ([Pages from a bundle](pages.md#pages-from-a-bundle)).
1757
+ - The script is a page script like any other: `page`, `ui` and `runtime` ([pages.md](pages.md)). The shipped
1758
+ schedules bundle is the example (`lib/samagotchi/bundles/schedules/`).
1759
+
1740
1760
  ## Stable for bundles
1741
1761
 
1742
1762
  Bundles normally use only `chi.*` and `ctx`; the constants, helpers and manifest
@@ -1789,7 +1809,7 @@ that calls it, or for a real external bundle that does. No method on the chance
1789
1809
  7. **The manifest schema**: the manifest's keys and shapes, as [A bundle with a plugin](#a-bundle-with-a-plugin)
1790
1810
  and [bundles.md](bundles.md) document them — `name`, `version`, `scope`, `description`, `files`, `hooks`,
1791
1811
  `trust_level`, `plugin`, `requires_chi`, `requires_chi_next` (shipped bundles only: the release tooling
1792
- reads it, chi doesn't), `needs`, `scripts`, `context_providers`, `block_renderers`, `includes`,
1812
+ reads it, chi doesn't), `needs`, `scripts`, `context_providers`, `block_renderers`, `pages`, `includes`,
1793
1813
  `session_settings`, `settings`. Those are `BundleManifest::KEYS`; `BundleManifest::SINCE` is the chi release each
1794
1814
  key or shape first worked in, and the docs don't copy it — the code is its truth.
1795
1815
  - A key keeps its meaning. A new key gets a `SINCE` entry and a CHANGELOG line, and an older chi says
@@ -1801,6 +1821,10 @@ that calls it, or for a real external bundle that does. No method on the chance
1801
1821
  There is no `api_version:`.
1802
1822
  - `block_renderers:`, the frame's `chi.defineRenderer` and `chi.features` (`regions`: `render` may return
1803
1823
  `{ svg, regions }`) ([Block renderers](#block-renderers)) are part of it. Neither is Ruby, so the scan below doesn't see them.
1824
+ - `pages:` and the page script DSL a bundle's page (or anyone's) is written in: `page`, `ui.text` / `ui.table` /
1825
+ `ui.walk`, and `runtime` with what it returns (`runtime.schedules`: its members, `rows`, `warning`,
1826
+ `unreadable_ids`, `Pages::Runtime::Unreadable`) ([pages.md](pages.md)). A page runs in the page runner, not as a
1827
+ plugin, so the scan below doesn't see it either.
1804
1828
 
1805
1829
  `Samagotchi::Testing::Boundary::ALLOWED` is the checked copy of items 1-6,
1806
1830
  and `Boundary::EXCEPTIONS` the per-bundle list of what each shipped bundle
data/docs/releasing.md CHANGED
@@ -13,7 +13,7 @@ can run the whole release; the user approves the notes before the tag and the
13
13
  and `rake release:check` enforce it). The system bundle upgrades itself when
14
14
  chi starts.
15
15
  - **Every other shipped bundle** (btw, check-in, guardrails, known-names,
16
- loop-guard, mcp, skills, slow-commands, agents-md, cost-cap, work-cap, source-links, github-pr, coordinator, claude-peer, codex-peer, ruby, model-notes, decision-card, inline-widgets, web-search, and the profiles core and dev) has its own semver in its
16
+ loop-guard, mcp, skills, slow-commands, agents-md, cost-cap, work-cap, source-links, github-pr, coordinator, claude-peer, codex-peer, ruby, model-notes, decision-card, inline-widgets, web-search, schedules, and the profiles core and dev) has its own semver in its
17
17
  `manifest.yml` and, when it needs a newer chi, a `requires_chi:` line. They never upgrade by themselves: users run
18
18
  `chi update` (all of them, with the gem) or `chi bundle upgrade NAME`. So:
19
19
  - a change to a bundle's files bumps that bundle's `version:`
data/docs/scheduling.md CHANGED
@@ -105,7 +105,11 @@ time zones.
105
105
 
106
106
  ## The busy check
107
107
 
108
- A cron tick cannot read a worker's memory, so the tick has two paths.
108
+ A cron tick cannot read a worker's memory, so the tick has two paths. What the
109
+ session's lifecycle state answers (deleting, stopped, a worker starting, leaving or
110
+ on a newer chi) comes from the lifecycle table (`SessionLifecycle::ALLOWED`,
111
+ [internals](internals/session-lifecycle.md)); the Bridge ask and the file checks
112
+ below are the busy check's own.
109
113
 
110
114
  **Being deleted** (a deleter holds the owner lock) comes before a stop marker.
111
115
  The skip is "the session is being deleted", and the next tick finds the session
@@ -197,6 +201,29 @@ tick is about an hour of patience, which is what the count really measures.
197
201
  `chi schedule enable` resets both counters too: a schedule turned back on gets its
198
202
  whole budget again, not one skip.
199
203
 
204
+ `chi schedule show ID` ends with the entry's last 20 outcomes, newest first, one
205
+ line each in local time: `fired`, `skipped`, `failed`, `missed`, your `enabled` and
206
+ `disabled`, a `chi schedule edit`'s `edited` (naming the fields), with the reason and
207
+ the session it acted on (a skip or
208
+ failure that used up the budget says `disabled: <reason>`). `list --format json` has
209
+ them as each entry's `history` (and an unpushed turn-off as `disabled_notice`).
210
+
211
+ `list --format json` gives each entry every stored field (with any key a newer chi
212
+ added), then `recurrence_words`, `skip_window`, and what a reader would otherwise work
213
+ out: `name` (the title, or the start of the prompt in quotes, as `list` names it),
214
+ `state` (`on`, `firing` while a firing is in flight, `off`) and `off_reason`, why an
215
+ off one is off: `owner` (your `disable`), `skips` or `failures` (a budget used up),
216
+ and for a `date` one-off its last outcome in the history: `done` (it fired),
217
+ `failures` (it failed) or `missed`; `off` when none of these says (an older file's
218
+ spent one-off). These come last, so a stored key of the same name never hides them
219
+ (`ScheduleDisplay.json`).
220
+
221
+ `chi schedule edit ID` changes a schedule in place (its title, prompt, recurrence,
222
+ catch-up or budgets; [CLI](cli.md#durable-schedules-chi-schedule)) and keeps its id,
223
+ session, mode, counters and history. A new recurrence starts its clock at the edit, as
224
+ an `add`'s does, and waits for a firing in flight to settle; a spent `date` one-off
225
+ stays disabled after an edit until you `enable` it.
226
+
200
227
  A recurring schedule the owner has disabled stays off when an in-flight skip or
201
228
  failure is recorded. A `date` one-off is the exception: a skip or failure puts its
202
229
  moment back and re-enables it. `run-now` still tries a disabled schedule, but a
@@ -204,13 +231,19 @@ refusal records its result without moving the skip or failure counters.
204
231
 
205
232
  The tick line that disabled an entry ends `(disabled)` (`chi schedule tick`,
206
233
  `run-now`), and `tick --format json` gives that outcome `"disabled": true`
207
- (`Schedules::Outcome#disabled`). When **chi web's** tick (`schedule.auto_tick`)
208
- disables a recurring schedule, the phone gets one notification
234
+ (`Schedules::Outcome#disabled`). When any tick — chi web's own (`schedule.auto_tick`),
235
+ a cron `chi schedule tick` or a `chi schedule run-now` — disables a recurring schedule
236
+ by its budget, the phone gets one notification
209
237
  ([Phone notifications](configuration.md#phone-notifications-web-push)): "Schedule
210
238
  disabled: Heartbeat", "skipped 12 times: <the last reason>" and the `chi schedule
211
- enable <id>` that turns it back on; a tap opens its session. It shows even with a chi
212
- web page in front and waits a day for a phone that is offline. Nothing notifies for a
213
- cron tick or `chi schedule run-now` (only chi web pushes), a `--dry-run`, your own
239
+ enable <id>` that turns it back on; a tap opens its session. The tick leaves a notice
240
+ on the entry in `schedules.json` (`disabled_notice`) and a running chi web pushes it:
241
+ right after its own tick, and within `schedule.tick_interval_minutes` for another
242
+ ticker's, with `schedule.auto_tick` off too. Only chi web pushes, so with no chi web
243
+ running the notice waits for one; a notice more than a day old, or one whose schedule
244
+ you enabled again, is dropped unpushed, and `chi schedule enable` clears it. It shows
245
+ even with a chi web page in front and waits a day for a phone that is offline. Two chi
246
+ webs on one state dir push it once. Nothing notifies for a `--dry-run`, your own
214
247
  `chi schedule disable`, or a `date` one-off (spent when it fires; one that then fails
215
248
  stays disabled with the failure in `chi schedule list`).
216
249
 
@@ -222,7 +255,9 @@ and `show`. A question does not time out with the 40-second firing step: the tur
222
255
  stays pending until its hard limit, derived from the schedule interval (at least
223
256
  10 minutes and at most 6 hours). If no outcome is known by then, it counts as a
224
257
  failure with reason `no outcome`. Failed, canceled and expired turns are not
225
- re-fired; `next_due` remains at the next occurrence.
258
+ re-fired; `next_due` remains at the next occurrence. The turn's outcome is read
259
+ from its files by `TurnOutcome.for` (`turn_outcome.rb`: its reply, its settled turn and
260
+ the words); `Schedules#resolve_pending` keeps the order: the reply, the question, the turn, expiry.
226
261
 
227
262
  A chain holds at most `schedule.max_per_session` enabled schedules (default 10) and
228
263
  the machine `schedule.max_total` (default 50); `schedule_task` and `chi schedule add`
@@ -267,6 +302,17 @@ its descendants, and archived sessions are outside the retention sweep
267
302
  count. The `anchor` is never pruned: it is the chain's root, the id the user and
268
303
  the coordinator remember, and `last:<anchor>` must keep resolving.
269
304
 
305
+ What a schedule protects (its anchor, its head and its resolved target, while it is
306
+ enabled) the retention sweep keeps and a delete refuses: schedules are
307
+ `SessionProtection`'s one protector (`Schedules::ProtectionPart`, [sessions.md](sessions.md)). It reads
308
+ `schedules.json` strictly: a file that is there but doesn't read (not JSON, a `schedules` member that isn't a list, a
309
+ file chi can't open) makes the sweep delete nothing that round (`retention_skipped`, retried at the next sweep
310
+ interval) and every delete refused, "schedules.json is unreadable (…): fix or remove it before deleting sessions". One
311
+ schedule that doesn't read does the same in its own words, "schedule entry <id> in schedules.json is unreadable
312
+ (lacks prompt): chi schedule cancel <id>, or fix the file, before deleting sessions": `chi schedule cancel <id>` removes
313
+ such a record by its id (`list` doesn't show it). The tick keeps its own rule (it refuses a file that isn't JSON and
314
+ skips a schedule that doesn't read).
315
+
270
316
  Schedule prune leaves the deleted links' background tasks running: a later link
271
317
  may still use a server started by an earlier link in the same project. Continue
272
318
  does not yet transfer task ownership.
@@ -303,7 +349,8 @@ Run it from systemd or launchd instead if you prefer a timer: the command is the
303
349
  same, `chi schedule tick`, and the lock makes overlapping runs harmless — the
304
350
  second one exits 0 with "no schedules due (another tick holds the lock)".
305
351
  `--dry-run` prints what would fire and changes nothing; `--format json` is the seam
306
- for scripts and for a future web UI. `chi schedule run-now` reserves the entry
352
+ for scripts; a page script reads the schedules in chi itself instead
353
+ (`runtime.schedules`, [pages.md](pages.md#reading-chis-data-runtime)). `chi schedule run-now` reserves the entry
307
354
  under the same lock before starting its action, so it cannot race a tick into a
308
355
  duplicate firing.
309
356
 
@@ -327,5 +374,21 @@ the configured interval, `chi schedule add`, `chi schedule list` and `schedule_t
327
374
  warn to turn `schedule.auto_tick` on or run `chi schedule tick` from cron; `--dry-run`
328
375
  and lock-held attempts do not refresh `last_tick_at`.
329
376
 
377
+ ## The Schedules page
378
+
379
+ `chi bundle install schedules` adds a **Schedules** page to chi web (`#/p/schedules`,
380
+ under the `pages` pill; `chi page run schedules` in a terminal) from the shipped
381
+ schedules bundle ([bundles.md](bundles.md#the-schedules-bundle)). It shows how many
382
+ schedules there are and in which state, with the tick warning when nothing ticks; each
383
+ schedule that is off and why ("turned off by you", "skipped 12 times (≈1 h at a 5 min
384
+ tick): busy", "failed 3 times: …", "one-off, done") with the `chi schedule enable ID`
385
+ that turns it back on (for a spent one-off, which has no next time, a `chi schedule
386
+ edit ID --date …` first); each entry of `schedules.json` that can't be read with the
387
+ `chi schedule cancel ID` that removes it; a table of every schedule (on and firing
388
+ first by next due, those with none last, then off; times in this machine's zone) and the recent runs, newest
389
+ first. It is read-only: ask chi, or use `chi schedule`, to change one. What it shows is
390
+ as of its last run: Refresh to update. A `schedules.json` that isn't JSON fails the
391
+ run with the store's own words, and the last good result stays.
392
+
330
393
  See [cli.md](cli.md) for the subcommands and flags, and
331
394
  [configuration.md](configuration.md) for the `schedule.*` settings.
data/docs/sessions.md CHANGED
@@ -31,7 +31,7 @@ unread messages reach the new link as one note); a delete removes them. See
31
31
  | `SAMAGOTCHI_SESSION_KEEP_STATUS` | (none) | CSV of statuses never auto-pruned |
32
32
  | `SAMAGOTCHI_SESSION_SWEEP_INTERVAL_HOURS` | `24` | Throttle lazy sweep |
33
33
 
34
- 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 its `*.json` and sidecar dir under the deleting owner lock, along with its plugin state, attached context, participant grants, System 1 runs and context-candidate records. Explicit delete and retention use the same cleanup path. Set both `DAYS=0` and `MAX=0` to retain forever.
34
+ 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 its `*.json` and sidecar dir under the deleting owner lock, along with its plugin state, attached context, participant grants, System 1 runs and context-candidate records. Explicit delete and retention use the same cleanup path. Set both `DAYS=0` and `MAX=0` to retain forever. A session something protects is kept whatever its age or place: an enabled schedule's anchor, head and resolved target (`SessionProtection.protected_ids`; schedules are its one protector, *Deleting a session*). A protector that can't answer stops the sweep's deletes for that round: a `schedules.json` that is there but doesn't read (not JSON, not a schedules document, a file chi can't open, or a schedule in it that doesn't read) protects everyone until it is fixed (one `retention_skipped` line per sweep interval, the next try at the next interval; the skeleton-only dirs and saved runs are still swept), and `chi sessions prune`/`clean` exit 1 with its words; a missing one is no schedules ([scheduling.md](scheduling.md#keeping-the-chain-short): `chi schedule cancel <id>` removes one schedule that doesn't read).
35
35
 
36
36
  Retention and manual prune/clean silently stop each deleted session's verified background tasks under its deleting lock, with `by: retention` (`stopped_by_retention`). Sessions are processed sequentially, their tasks in parallel. Unverified or unreadable tasks are not signalled; stops and failures are logged per task as `retention_task_stopped` / `retention_task_not_stopped`, without commands. A scratch session's own cleanup does the same with `by: scratch` and `scratch_task_stopped` / `scratch_task_not_stopped`.
37
37
 
@@ -55,7 +55,7 @@ A new session field is one `Session::FIELDS` row, plus a `SessionSetup` member w
55
55
 
56
56
  **Stopping a worker:** `chi sessions stop ID` marks the session stopped (an `<id>/stopped` file holding when, `{"at"}`, not a session.json field, which a worker saving at that moment would write over; a resume removes it), sends its worker TERM, then CONT (a stopped worker, SIGSTOP or Ctrl-Z, runs its TERM handler only once continued, so it leaves at once instead of outliving the wait), 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 ⚙ panel, against a 0.39.0 worker) says the same, not `not a session command` (`SessionCommandCatalog.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. Before that stop, the page's `stop session` button asks what the worker still has that the user can stop: `GET /api/sessions/:id/holds` (owner-only) answers the worker's `holds` — the same list the exit ask's 409 carries — always an array, empty when there is no live worker, one from before the route, or one that did not answer. With something, a sheet (a bottom sheet on a phone, a popover at the button otherwise — `stop_sheet.js`) lists them, at most three of a kind named and `+N more`, and offers **Stop all** (`POST /api/sessions/:id/holds/stop` → `HoldStop#stop_all`, then the stop), **Stop session only** (the stop exactly as it was) or **Cancel**. Nothing to ask about is the plain confirm, one tap as before. Both buttons stay because a stop only TERMs the worker: its reminders die with it (a stop also deletes a restart's `reminders.json` no successor took yet, so a later resume doesn't bring them back), while a background task and a delegate are their own processes and keep running. A delete or an archive differs: it never leaves a task the session started running unasked (*Deleting a session*: refused, or `--stop-tasks` stops it), where Stop's "Stop session only" does. 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.
57
57
 
58
- **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 one-turn `chi --non-interactive` run has (`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. A session whose worker is being started (the `<id>/starting` marker) is refused or stopped the same way, and the files go while the delete holds `owner.lock`, so a worker can't take the session in the middle; a worker that still had it loaded doesn't write it back. While the delete holds it (kind `deleting`), every delivery is refused: a message (`chi send`, the web, a schedule, a delegate, a plugin, a participant) gets 409 `session_deleting`, "the session is being deleted", and a send that read the session before the lock and wrote after it takes its file back and says the same (unless a worker took the file meanwhile: then it is delivered); a note is refused as for a gone session, and a delegate's ring, a push and a fresh verdict are not written. The web asks in one sheet first (*Session chains* → Deleting a link, the page side in docs/internals/web-frontend.md, The delete sheet): what goes, what is stopped first and what stays, one button naming the outcome, no browser confirm. 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, unarchived, moved_back}`; `deleted` and `left_running` too; 409 with the refusal's reason as its code; 404). A schedule's target, anchor or head is refused (`scheduled`, naming the schedule): deleting a head would make the schedule fall back to an archived link. A user's delete (these three, `revive_previous:` and `refuse_continuing:` on `delete_session`) goes through `SessionChain::Delete.link` for every session, chained or not (*Session chains* → Deleting a link): **its delegates follow one rule** (`SessionManager::DELEGATES`): the stopped ones (archived ones too) go with it, deepest first, and a running one stays with its whole subtree, its file untouched; a stopped delegate with a running child goes and leaves that child without a parent. The delegates that would go bring their own refusals: one that is a schedule's target, anchor or head refuses the delete (`scheduled`, naming the delegate), as does one in a one-turn run, one a worker took after the plan was made (`worker_running`), and their running background tasks (below); their participant grants end with them. The result lists them in `deleted` (the session last) and the running ones with their subtrees in `left_running`; the CLI's line and `/exit --delete` say how many went. It is also refused while a continue of the session, or of the previous link it would bring back, is under way (`continuing`), and deleting a chain's newest link undoes its continue (D2/D5 below), the ids it brought back in `unarchived` and the delegates it gave back in `moved_back`. A previous link brought back is aged for retention from then on (`unarchived_at`) and has no participants (the archive ended them, and an unarchive doesn't bring them back). The scratch cleanup, the empty-session discard, the failed-continue rollback and a schedule's prune delete plainly (`delete_resolved`: they pass neither keyword and take no delegates); the retention sweep removes files directly (`remove_files`). **Background tasks still running:** a user's delete (these three, and `chi sessions delete`) asks first when tasks the deleted sessions started (their `task_create` tasks and the `execute` runs handed over at their limit, read from each session's transcript and `<working_directory>/tmp/tasks/<id>/task.json`: `SessionManager::RunningTasks`) still run: it is refused before anything changes (`tasks_running`), and the CLI's `--stop-tasks`, the web's `stop_tasks=true` (the delete's and the chain delete's query; with `&tasks=<id,id>`, the ones the sheet named, only those: `RunningTasks::Policy.stop(only:)`, see *Deleting a link*) stop them first (`stopped_by_user`, TERM then KILL after 3 s, all at once, about 4 s at most), under the deleting lock, after the starting-marker re-check, so no worker takes the session meanwhile; that last check refuses a task that started since the first (`worker_stopped: true` when the worker was stopped by then). One rule, fail-safe: a task whose pid still leads the group chi started at the start time it recorded is *running*; one whose record has no start time (before 2026-10-09) or whose start time reads nothing now, while its pid leads a group, is *unverified* ("may still be running"); a `task.json` that doesn't read is *unreadable*. All three refuse; a stop signals only the running ones (an unverified pid may belong to another process now) and reports the rest as not stopped, then deletes. A record left marked running whose pid is gone (a crashed worker's) isn't listed. The 409 is `{error: "tasks_running", reason, op: "delete", detail, session_id, tasks: {running, unverified, unreadable}, worker_stopped, stopped_worker, stopped_workers}` (`stopped_worker`: whose worker the delete stopped before the last check refused; `stopped_workers`: every worker the delete stopped), each task `{session_id, id, command}` (`links` too for a chain delete's other links); the 200 gains `tasks_stopped`, `tasks_not_stopped` and `tasks_ended` (empty unless stopped). A session's file gone (its folder left) has no tasks. The retention sweep and scratch cleanup stop verified tasks silently; the empty-session discard, failed-continue rollback and schedule prune leave them running.
58
+ **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 one-turn `chi --non-interactive` run has (`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. A session whose worker is being started (the `<id>/starting` marker) is refused or stopped the same way, and the files go while the delete holds `owner.lock`, so a worker can't take the session in the middle; a worker that still had it loaded doesn't write it back. While the delete holds it (kind `deleting`), every delivery is refused: a message (`chi send`, the web, a schedule, a delegate, a plugin, a participant) gets 409 `session_deleting`, "the session is being deleted", and a send that read the session before the lock and wrote after it takes its file back and says the same (unless a worker took the file meanwhile: then it is delivered); a note is refused as for a gone session, and a delegate's ring, a push and a fresh verdict are not written. The web asks in one sheet first (*Session chains* → Deleting a link, the page side in docs/internals/web-frontend.md, The delete sheet): what goes, what is stopped first and what stays, one button naming the outcome, no browser confirm. 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, unarchived, moved_back}`; `deleted` and `left_running` too; 409 with the refusal's reason as its code; 404). A schedule's target, anchor or head is refused (`scheduled`, naming the schedule): deleting a head would make the schedule fall back to an archived link. Who refuses a delete is one question, `SessionProtection` (`session_protection.rb`, beside `SessionStores`, which says what a delete removes): every protector in `PROTECTORS` is asked (`hold` for a session, `chain_hold` for a chain, `protected_ids` for retention), schedules the one there is (`Schedules::ProtectionPart`), and the first hold is the refusal (`SessionManager.refuse_protected!`, `DeleteRefused` with the hold's reason and its `schedule_id`). It fails closed: a protector that raises is logged (`protector_failed`) and the error raised, so nothing is deleted; a store that is there but doesn't read (`SessionProtection::Unreadable`) refuses every delete, `protection_unreadable`, "schedules.json is unreadable (…): fix or remove it before deleting sessions" (the delete sheet shows it as the choice's refusal). An archive asks no protector. A user's delete (these three, `revive_previous:` and `refuse_continuing:` on `delete_session`) goes through `SessionChain::Delete.link` for every session, chained or not (*Session chains* → Deleting a link): **its delegates follow one rule** (`SessionManager::DELEGATES`): the stopped ones (archived ones too) go with it, deepest first, and a running one stays with its whole subtree, its file untouched; a stopped delegate with a running child goes and leaves that child without a parent. The delegates that would go bring their own refusals: one that is a schedule's target, anchor or head refuses the delete (`scheduled`, naming the delegate), as does one in a one-turn run, one a worker took after the plan was made (`worker_running`), and their running background tasks (below); their participant grants end with them. The result lists them in `deleted` (the session last) and the running ones with their subtrees in `left_running`; the CLI's line and `/exit --delete` say how many went. It is also refused while a continue of the session, or of the previous link it would bring back, is under way (`continuing`), and deleting a chain's newest link undoes its continue (D2/D5 below), the ids it brought back in `unarchived` and the delegates it gave back in `moved_back`. A previous link brought back is aged for retention from then on (`unarchived_at`) and has no participants (the archive ended them, and an unarchive doesn't bring them back). The scratch cleanup, the empty-session discard, the failed-continue rollback and a schedule's prune delete plainly (`delete_resolved`: they pass neither keyword and take no delegates); the retention sweep removes files directly (`remove_files`). **Background tasks still running:** a user's delete (these three, and `chi sessions delete`) asks first when tasks the deleted sessions started (their `task_create` tasks and the `execute` runs handed over at their limit, read from each session's transcript and `<working_directory>/tmp/tasks/<id>/task.json`: `SessionManager::RunningTasks`) still run: it is refused before anything changes (`tasks_running`), and the CLI's `--stop-tasks`, the web's `stop_tasks=true` (the delete's and the chain delete's query; with `&tasks=<id,id>`, the ones the sheet named, only those: `RunningTasks::Policy.stop(only:)`, see *Deleting a link*) stop them first (`stopped_by_user`, TERM then KILL after 3 s, all at once, about 4 s at most), under the deleting lock, after the starting-marker re-check, so no worker takes the session meanwhile; that last check refuses a task that started since the first (`worker_stopped: true` when the worker was stopped by then). One rule, fail-safe: a task whose pid still leads the group chi started at the start time it recorded is *running*; one whose record has no start time (before 2026-10-09) or whose start time reads nothing now, while its pid leads a group, is *unverified* ("may still be running"); a `task.json` that doesn't read is *unreadable*. All three refuse; a stop signals only the running ones (an unverified pid may belong to another process now) and reports the rest as not stopped, then deletes. A record left marked running whose pid is gone (a crashed worker's) isn't listed. The 409 is `{error: "tasks_running", reason, op: "delete", detail, session_id, tasks: {running, unverified, unreadable}, worker_stopped, stopped_worker, stopped_workers}` (`stopped_worker`: whose worker the delete stopped before the last check refused; `stopped_workers`: every worker the delete stopped), each task `{session_id, id, command}` (`links` too for a chain delete's other links); the 200 gains `tasks_stopped`, `tasks_not_stopped` and `tasks_ended` (empty unless stopped). A session's file gone (its folder left) has no tasks. The retention sweep and scratch cleanup stop verified tasks silently; the empty-session discard, failed-continue rollback and schedule prune leave them running.
59
59
 
60
60
  <a id="archiving-a-session"></a>**Archiving a session:** `chi sessions archive ID...`, `/archive` in a terminal and the Web UI's `archive` go through `SessionManager.archive_session`: it writes `<id>/archived` (`{"archived_at": …}`; not a session.json field, which every save rewrites) for the session and its delegated children. `Session.list` leaves archived sessions out unless `include_archived:`, and everything built on it follows: `chi sessions list` (`--archived` shows them, `[archived]`), the summaries (`--live`, json/tsv, the desktop panel, the agent's `list_sessions`) and the retention prune, which neither deletes nor counts them against `max_count`. `children_of` (max_children, `delegate_result`) still sees them, and `--resume`/`--attach ID`, `chi send`/`chi note ID` reach them as before, except that `chi send` and `chi note` to a link that was continued go to its chain's newest link (*Session chains*). Refused: a turn running in the session or in one of its children (naming the child; 409 `busy` on the web), a prompt queued in `input/` (it would run on the next resume) or a live worker's step-limit question still open, in it or a child (409 `busy` too), a one-turn `chi --non-interactive` run owning it (`owned_by_tui`), a `chi scratch` session (`scratch`). A live idle worker is stopped first, and the marker is written even if it is still shutting down. Input a human typed brings a session back (`{"unarchived_at": …}`): a prompt from the web, an attached terminal or `chi send` (origin `web:*`, `tui:*`, `cli:send`, its own first prompt: `spawn:first`; input naming no sender doesn't), steering merged into a running turn, a `/continue` answer or a question answered (the step-limit question too); a delegate's follow-up, a plugin's turn, a reminder, the idle recap, a note and just opening it don't. A child's input brings only the child back. The retention sweep ages an unarchived session from when it was unarchived, so unarchiving an old session doesn't let the next sweep take it. `/archive` leaves the session first, as `/exit` does, then archives it: in an attached terminal the worker is asked to exit (one that stays up for another UI is stopped by the archive; a running turn, a queued prompt or an open step-limit question refuses it), and an empty session is discarded instead; `chi scratch` refuses it. Typed while a turn runs, `/archive` waits for the turn's end (an attached terminal for the commands queued behind it too) and then archives. `chi sessions unarchive ID...` and the web's `unarchive` bring back the whole family. **Background tasks still running** in the session or a delegate refuse an archive as they refuse a delete (*Deleting a session*: the same rule and listing), before anything changes: 409 `tasks_running` (not `busy`, so an older client doesn't say "a turn is running"), `op: "archive"`. The web's Archive stays one tap (the toast's Undo) when nothing is in the way; its 409 opens the delete sheet in Archive's words (what is hidden, the tasks it stops first, `Stop them and archive`, which sends the tasks it named), and any other refusal is that sheet's "Can't archive … now". The 409 also says `sessions`: how many sessions the archive would hide (it and its delegates, across every project), plus `stopped_workers` (every stopped worker id). The refusal words name one stopped worker as "its worker was stopped" or "9d0e1f2a's worker was stopped", and several as "N workers were stopped: its own, 9d0e1f2a". `chi sessions archive --stop-tasks`, or the archive route's body `{"stop_tasks": true}` (with `"tasks": [ids]` only those; a running one not named refuses, `tasks_running`, nothing archived: up front before any worker is stopped, and again after the workers let go), stops them first: after the tree's live workers were stopped and let go (or the wait ran out: such a worker's own waiter then records the stop from the `stop_requested_by` flag on disk), and before the marker; the 200 gains `tasks_stopped`, `tasks_not_stopped` and `tasks_ended`. An archive takes no lock for it. Undo (unarchive) doesn't restart them. Web routes: `POST /api/sessions/:id/archive` (200 `{status, session_id, archived, stopped, discarded}`, ids) and `POST /api/sessions/:id/unarchive` (200 `{status, session_id, unarchived}`). The session hub and `GET /api/sessions` keep archived sessions, `archived: true` in their summaries: the page leaves them out of the strip, its count and the list at render, and the `archived` toggle beside the all-sessions search shows them, dimmed with an `archived` badge. The info bar marks a session whose live worker runs an older chi than the newest installed (`worker_version` in its summary): `chi 0.18.1 · Restart`, which asks to confirm and calls `POST /api/sessions/:id/restart` (a refusal's reason shows in a toast); a worker from before restarts shows `chi sessions stop ID` instead. The info bar reads `continue → · archive · stop · delete` (`unarchive` on an archived one; no `continue →` on a link continued already, see *Session chains*; `continue →` is a plain button like `archive`, in the accent on an archived session, where it is the thing to do); archiving the open session stays on it, with an Undo toast.
61
61
 
@@ -408,8 +408,8 @@ A session can **continue** an earlier one: "the next run of the same routine" (a
408
408
  - **The page:** the info bar's `continue →` continues the open session (`continues: <id>, idle: true`; it reads `continuing…` through the recap wait) and opens the new link with an opener in the composer, not sent (a draft written in this session stays instead; one carried over from another session or the start page gives way to the opener, and the toast's **Restore draft** brings it back): the previous link's own first typed message when that link continues one (the 201's `opener`; a `!shell` line isn't taken), else `Continue where we left off.`. So whatever you sent on the last link comes back on the next one, and editing it once changes it for every later link; the previous link is archived, a toast says so (`Continued 3fa2c1d0 in 4b7e9a01; 3fa2c1d0 is archived; 1 open delegate moved`), and a moved delegate's card folds under the new link. When the previous link's model's host is gone the link starts on the default model and the toast carries the server's `model_warning` too, and then stays until closed. A fresh link (one with no turn yet, however it was made: `continue →`, `chi send --new --continues`, a reload) shows the model picker in its composer with the link's model, until its first turn starts (a failed or canceled first turn ends it too); a pick runs `/model <name>` in the link (a command bubble; the info bar follows) and changes neither the start page's remembered model nor its Recent list, and a Send right after it waits for the `/model` to be taken. A link a one-turn `chi --non-interactive` run has, or one continued already, shows none. A link continued already has no `continue →`: its `→` link goes to the next link (a stale page's continue answered 409 `continued` opens `next_id`). A continue refused for open delegates that can't move shows a toast that names each one (its preview and what the server says to do: `older chi worker 0.46.1; restart it`, `in use by a one-turn chi --non-interactive run or an older chi's terminal; let it finish`), each a link to its page; any other refusal shows the server's words. The info bar of a link carries `← 2026-10-08` (the previous link's day) and, on an earlier link, `2026-10-10 →`, each a link with its id, its full start (`2026-10-10 14:32`) and its preview in the tooltip; a neighbour that started on the open link's own day shows its time instead (`← 09:12`, `14:32 →`). A link made by `continue →` inherits the previous link's preview (and its title, if it has one) when it is created, so its card keeps its chain's title whatever opener it is sent, and its top bar and browser tab show its card's title. A link with a recap of its own reads its auto title, else its recap's first sentence, instead (a link without one reads its chain's name, and a first link's recap names its chain). A link whose title is the opener and nothing else (a `chi send --new --continues` link whose first message is `Continue where we left off.`) shows its chain's first link's title instead, on its card, in the popover and in its info bar's title line, display only (its messages and data keep the opener; its own title when the first link is gone). The lists fold a chain into the card of its latest listed link, whatever the earlier links' archived state (typing in an archived link brings it back, folded there still), their delegates with them: the card's `↩ #N` chip (`#N of M` when later links aren't listed; its tooltip `Session chain: link N of M`) opens a popover of the other links, newest first (`#N`, its date, with its time (`2026-10-17 14:32`) when another link of the chain started the same day, its own title when it differs from the card's (a link inherits its chain's, so mostly it doesn't), else its recap's first sentence, else its preview, `archived`), a click opens one; the chip shows a live dot or `waiting` for a link that has a worker or waits on the user, and a waiting link puts its chain's card first. All sessions folds nothing while a search is typed or the `archived` toggle is on: every link is a card of its own then, its chip `#N of M`, with its own delegates folded in, so a search finds an earlier link and its delegates.
409
409
  - **CLI:** `chi send --new --continues (ID|PREFIX|last:ID) [--model M] [-m TEXT]` (see *Starting a session*).
410
410
  - **Which link:** an id, a unique prefix, or `last:<id or prefix>`, the latest link of the chain that session is in. It walks the links that still exist: a deleted link in the middle (`chi sessions delete`, the retention sweep of an unarchived one) ends the walk before it, so `last:` of an earlier link then names that earlier link, and continuing it starts a second line beside the links after the gap; name a link after the gap instead. A trigger names the chain this way: a person's message to an archived link that nobody continued brings it back (a human's input), it doesn't continue the chain; an unattended sender's is refused (*Who may wake a session*). That is the rule: `chi send` and a note go to the chain's newest link (*Sending a message*, *Context notes*); the web composer, a participant's turn and `delegate(session:)` go to the link they name. One walker answers every one of these (`SessionChain::Links`: one session listing, archive flags read once; the earliest created successor wins a fork a hand edit made, a loop ends where it would repeat), with two rules on it, not unified on purpose: `last:` and a schedule take the chain's **end** (`Links#latest`: a continue refuses a link continued already, and a schedule never un-archives a chain you retired), `chi send` and a note the **first link not archived** (`Links#first_live`: a person's input goes where the chain is live, and brings back an archived end). A chain pruned by a schedule keeps its `anchor`: the kept window's oldest link still names a link that is gone, so the walk simply stops there; the page's chip counts from the surviving root (pruning renumbers the links) and its popover says `earlier links pruned` where the walk stops, rather than linking to a deleted session.
411
- - **Deleting a link** (`SessionChain::Delete`; the CLI, the web and `/exit --delete`): a link of a chain is deleted alone by default, and what follows depends on where it is. **The newest link** (nobody continues it, it isn't archived itself, the link before it is archived) undoes its continue: the delegates the continue moved go back to the previous link, finished ones too (`continued_from.json`'s `moved_ids`, still under the link; without the record, its children whose `parent.json` says they came from the previous link), each with a note saying the link was deleted; every note waiting in the link but the continue's own goes back too (`NoteMove.give_back`); then the previous link and what the continue archived with it come back to the lists (without the record, the previous link alone). The previous link is **not started**: reports a moved-back delegate left wait for its next start. What dies with the link: its turns, context sources, objectives edits, session approvals and participant grants (the previous link kept its own copies). Every refusal comes before anything goes: the link's delegates that would go are checked too (a schedule on one, a one-turn run), and they and the link are removed (and the undo runs) only under their `owner.lock`s (kind deleting, taken without waiting, so a worker that took one refuses it); a moved delegate that can't go back (`ChildMove.unmovable`: an older chi's worker, a one-turn run) refuses it (`delegate_unmovable`, naming it), as does a worker that took the link after the stop. Deleting the links one after another undoes each continue by its own record. **A middle link** splits the chain (the earlier links stay archived, the later ones go on as their own chain); nothing is undone or re-linked. A link whose chain is all archived stays archived around the gap. **The link's own delegates** (any position) follow one rule, `SessionManager::DELEGATES` (`SessionManager::DelegateRule`; one shared preview row snapshot): a running one (a worker owns it or is starting for it, a one-turn run; a saved `running` with no worker is a crashed run, and goes) is left running with its whole subtree, its file untouched (its parent's id names a deleted session; its reports are refused as for any gone parent); every other node under the link goes, deepest first, before the link. A session in no chain (a link alone past a gap too) is a link with nothing to undo: its delegates follow the same rule (*Deleting a session*). **The web asks first**: chi web's delete sheet reads `GET /api/sessions/:id/delete_plan` (`:owner`; no query or body; no-store) for any session, `SessionChain::Delete::Preview` over the same `LinkPlan`/`ChainPlan` and checks the delete runs (`check_link!`, `check_chain!`): the link's place in the chain, what comes back and goes for each choice, and each choice's refusal now (a schedule on the chain, the link or a delegate that would go, a continue under way, a one-turn run, a moved delegate that can't go back and whether the page can stop it). A session in no chain answers `{chained: false, session_id, link}`, the same `link` a chained one gets (`previous_id` null, `earlier`/`later` 0, `next_id` null); an older page reads only `chained`. Each choice (`link`, `chain`) also carries `delegates` (`{delete: [ids], leave: [{id, running}]}`; `running` false for a stopped session left under a running delegate, beside the counts `deletes` and `left_running`), `tasks` (`{running, unverified, unreadable}`: `RunningTasks.of` the ids the choice deletes) and `worker` (a link's: `"turn"` when a worker owns it and a turn runs, `"idle"` when a worker owns it or is starting, null; a chain's `{idle, turn}` counts over its links). No titles: the page names each id from its own list. One shared session-row snapshot serves the link and chain choices; Links lists on its own, and a folder whose session file is gone answers too (the delete still takes it). Each call read-only checks each link's continue lock (a session in no chain too), and writes nothing else but the listing's own repair of an interrupted continue; an unknown id is the usual 404. chi web lists `delete_plan_all` in `/api/info`'s features (`Web::App::DELETE_PLAN_FEATURE`). **The tap stops only what the sheet named**: whenever the choice lists any task, the page sends `stop_tasks=true&tasks=<id,id>`, the choice's running ids (`tasks=` when it has only unverified or unreadable records, which a stop never signals, so such a session can still be deleted); archive's sheet sends `{"stop_tasks": true, "tasks": [ids]}` in its body. `tasks` works only with `stop_tasks`; without it, it is ignored, and `stop_tasks` without `tasks` (the CLI's `--stop-tasks`, an older page) takes every running task the operation can stop. A running task not named refuses (`tasks_running`), and the sheet reads the plan again and shows it with "changed; check again". A chain stops link by link: a link refused that way leaves it and the earlier links whole. The moved count is each moved delegate with its subtree (they come back with it), and a `delegate_unmovable` 409 says `stoppable` itself. Each choice carries a `digest` (`SessionChain::Delete.digest`: the sorted ids it deletes, leaves, revives and moves back); the sheet's DELETE sends it as `expect=`, and the delete compares it with the plan it runs (a link's under its holds, as `delete_link` starts; the chain's before its checks) and refuses `plan_changed` (409) before anything changes; a session in no chain is compared the same way (a sheet shown for a link whose chain is gone since deletes when its ids still match). Without `expect` (the CLI, an older page) it deletes as it plans then.
412
- **The whole chain** (`chi sessions delete --chain`, `DELETE /api/sessions/:id/chain`, `SessionManager.delete_chain`): every link reachable from this one on its branch, newest first, each with its delegates by the same rule. Refused before anything goes when a schedule is on the chain (`chain_scheduled`) or on a delegate that would go (`scheduled`), a link or such a delegate is in a one-turn run, a continue of a link is under way (`continuing`), without `force` a link has a worker, or a link's worker outlives the stop's wait: every link is checked and stopped before the first is deleted, and each then goes under the owner locks of it and its delegates. A chain with a gap before this link reaches only the part after the gap: the earlier links stay archived (`cut_off: true`). A link refused part-way (a worker that took it after its stop) leaves it, its delegates and the earlier part of the chain whole, its new end back in the lists, and the result lists what was left.
411
+ - **Deleting a link** (`SessionChain::Delete`; the CLI, the web and `/exit --delete`): a link of a chain is deleted alone by default, and what follows depends on where it is. **The newest link** (nobody continues it, it isn't archived itself, the link before it is archived) undoes its continue: the delegates the continue moved go back to the previous link, finished ones too (`continued_from.json`'s `moved_ids`, still under the link; without the record, its children whose `parent.json` says they came from the previous link), each with a note saying the link was deleted; every note waiting in the link but the continue's own goes back too (`NoteMove.give_back`); then the previous link and what the continue archived with it come back to the lists (without the record, the previous link alone). The previous link is **not started**: reports a moved-back delegate left wait for its next start. What dies with the link: its turns, context sources, objectives edits, session approvals and participant grants (the previous link kept its own copies). Every refusal comes before anything goes: the link's delegates that would go are checked too (a schedule on one through `SessionProtection.hold`, a one-turn run), and they and the link are removed (and the undo runs) only under their `owner.lock`s (kind deleting, taken without waiting, so a worker that took one refuses it); a moved delegate that can't go back (`ChildMove.unmovable`: an older chi's worker, a one-turn run) refuses it (`delegate_unmovable`, naming it), as does a worker that took the link after the stop. Deleting the links one after another undoes each continue by its own record. **A middle link** splits the chain (the earlier links stay archived, the later ones go on as their own chain); nothing is undone or re-linked. A link whose chain is all archived stays archived around the gap. **The link's own delegates** (any position) follow one rule, `SessionManager::DELEGATES` (`SessionManager::DelegateRule`; one shared preview row snapshot): a running one (a worker owns it or is starting for it, a one-turn run; a saved `running` with no worker is a crashed run, and goes) is left running with its whole subtree, its file untouched (its parent's id names a deleted session; its reports are refused as for any gone parent); every other node under the link goes, deepest first, before the link. A session in no chain (a link alone past a gap too) is a link with nothing to undo: its delegates follow the same rule (*Deleting a session*). **The web asks first**: chi web's delete sheet reads `GET /api/sessions/:id/delete_plan` (`:owner`; no query or body; no-store) for any session, `SessionChain::Delete::Preview` over the same `LinkPlan`/`ChainPlan` and checks the delete runs (`check_link!`, `check_chain!`): the link's place in the chain, what comes back and goes for each choice, and each choice's refusal now (a schedule on the chain, the link or a delegate that would go, a continue under way, a one-turn run, a moved delegate that can't go back and whether the page can stop it). A session in no chain answers `{chained: false, session_id, link}`, the same `link` a chained one gets (`previous_id` null, `earlier`/`later` 0, `next_id` null); an older page reads only `chained`. Each choice (`link`, `chain`) also carries `delegates` (`{delete: [ids], leave: [{id, running}]}`; `running` false for a stopped session left under a running delegate, beside the counts `deletes` and `left_running`), `tasks` (`{running, unverified, unreadable}`: `RunningTasks.of` the ids the choice deletes) and `worker` (a link's: `"turn"` when a worker owns it and a turn runs, `"idle"` when a worker owns it or is starting, null; a chain's `{idle, turn}` counts over its links). No titles: the page names each id from its own list. One shared session-row snapshot serves the link and chain choices; Links lists on its own, and a folder whose session file is gone answers too (the delete still takes it). Each call read-only checks each link's continue lock (a session in no chain too), and writes nothing else but the listing's own repair of an interrupted continue; an unknown id is the usual 404. chi web lists `delete_plan_all` in `/api/info`'s features (`Web::App::DELETE_PLAN_FEATURE`). **The tap stops only what the sheet named**: whenever the choice lists any task, the page sends `stop_tasks=true&tasks=<id,id>`, the choice's running ids (`tasks=` when it has only unverified or unreadable records, which a stop never signals, so such a session can still be deleted); archive's sheet sends `{"stop_tasks": true, "tasks": [ids]}` in its body. `tasks` works only with `stop_tasks`; without it, it is ignored, and `stop_tasks` without `tasks` (the CLI's `--stop-tasks`, an older page) takes every running task the operation can stop. A running task not named refuses (`tasks_running`), and the sheet reads the plan again and shows it with "changed; check again". A chain stops link by link: a link refused that way leaves it and the earlier links whole. The moved count is each moved delegate with its subtree (they come back with it), and a `delegate_unmovable` 409 says `stoppable` itself. Each choice carries a `digest` (`SessionChain::Delete.digest`: the sorted ids it deletes, leaves, revives and moves back); the sheet's DELETE sends it as `expect=`, and the delete compares it with the plan it runs (a link's under its holds, as `delete_link` starts; the chain's before its checks) and refuses `plan_changed` (409) before anything changes; a session in no chain is compared the same way (a sheet shown for a link whose chain is gone since deletes when its ids still match). Without `expect` (the CLI, an older page) it deletes as it plans then.
412
+ **The whole chain** (`chi sessions delete --chain`, `DELETE /api/sessions/:id/chain`, `SessionManager.delete_chain`): every link reachable from this one on its branch, newest first, each with its delegates by the same rule. Refused before anything goes when a schedule is on the chain (`chain_scheduled`, `SessionProtection.chain_hold`) or on a delegate that would go (`scheduled`), a link or such a delegate is in a one-turn run, a continue of a link is under way (`continuing`), without `force` a link has a worker, or a link's worker outlives the stop's wait: every link is checked and stopped before the first is deleted, and each then goes under the owner locks of it and its delegates. A chain with a gap before this link reaches only the part after the gap: the earlier links stay archived (`cut_off: true`). A link refused part-way (a worker that took it after its stop) leaves it, its delegates and the earlier part of the chain whole, its new end back in the lists, and the result lists what was left.
413
413
 
414
414
  ## Schedules
415
415
 
data/docs/testing.md CHANGED
@@ -683,7 +683,7 @@ that reopens the namespace and nothing else does (then firing the autoload loads
683
683
  four kinds: a part naming its host's value constants (`Samagotchi::Engine::[A-Z][A-Z_]*` in
684
684
  `engine/*.rb`: the host requires the part, so a require back is a cycle; the glob leaves classes a part
685
685
  names from elsewhere checked), one-off host pairs (`ToolRun` in `tool_response.rb`), the namespace
686
- autoloads that load whole (`SessionManager`, `Objectives`, `Config`), and `Plugin::Http`'s (its base file
686
+ autoloads that load whole (`SessionManager`, `Objectives`, `Config`, `Schedules`), and `Plugin::Http`'s (its base file
687
687
  loads the reopening file only inside a method, and that file requires the base file first). The summary counts the entries that
688
688
  matched and those that match nothing: delete a stale one.
689
689