samagotchi 0.190.0 → 0.191.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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +38 -1
  3. data/docs/architecture.md +8 -7
  4. data/docs/cli.md +6 -3
  5. data/docs/context.md +9 -2
  6. data/docs/guardrails.md +5 -1
  7. data/docs/internals/attached-context.md +3 -1
  8. data/docs/internals/peers.md +15 -3
  9. data/docs/internals/remote-sessions.md +2 -2
  10. data/docs/internals/session-lifecycle.md +213 -12
  11. data/docs/peers.md +3 -1
  12. data/docs/plugins.md +2 -1
  13. data/docs/scheduling.md +31 -7
  14. data/docs/sessions.md +11 -11
  15. data/docs/sub-agent.md +1 -1
  16. data/lib/samagotchi/bridge.rb +1 -1
  17. data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
  18. data/lib/samagotchi/bundles/known-names/plugin.rb +17 -1
  19. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  20. data/lib/samagotchi/context_command.rb +5 -1
  21. data/lib/samagotchi/context_sources.rb +28 -0
  22. data/lib/samagotchi/continue_intent.rb +23 -43
  23. data/lib/samagotchi/hold_stop.rb +5 -2
  24. data/lib/samagotchi/participants.rb +13 -5
  25. data/lib/samagotchi/peer_say.rb +29 -14
  26. data/lib/samagotchi/plugin/sessions.rb +9 -5
  27. data/lib/samagotchi/schedule_command.rb +5 -1
  28. data/lib/samagotchi/schedules/prune_part.rb +138 -38
  29. data/lib/samagotchi/schedules.rb +29 -6
  30. data/lib/samagotchi/send_command.rb +6 -6
  31. data/lib/samagotchi/session_access/ask.rb +9 -3
  32. data/lib/samagotchi/session_access/exchange.rb +6 -2
  33. data/lib/samagotchi/session_access/local.rb +104 -31
  34. data/lib/samagotchi/session_access/remote.rb +10 -9
  35. data/lib/samagotchi/session_access/remote_peers.rb +2 -2
  36. data/lib/samagotchi/session_access.rb +17 -7
  37. data/lib/samagotchi/session_chain/delete.rb +100 -47
  38. data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
  39. data/lib/samagotchi/session_chain/links.rb +68 -9
  40. data/lib/samagotchi/session_chain/successor.rb +78 -0
  41. data/lib/samagotchi/session_continue.rb +108 -40
  42. data/lib/samagotchi/session_inbox.rb +1 -1
  43. data/lib/samagotchi/session_inbox_delivery.rb +113 -26
  44. data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
  45. data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
  46. data/lib/samagotchi/session_lifecycle.rb +13 -2
  47. data/lib/samagotchi/session_manager/delivery.rb +61 -23
  48. data/lib/samagotchi/session_manager/emptiness.rb +5 -2
  49. data/lib/samagotchi/session_manager/lifecycle.rb +50 -14
  50. data/lib/samagotchi/session_manager/removal.rb +65 -13
  51. data/lib/samagotchi/session_manager/spawn.rb +14 -6
  52. data/lib/samagotchi/session_retention.rb +28 -7
  53. data/lib/samagotchi/session_transition.rb +315 -0
  54. data/lib/samagotchi/sessions_command.rb +1 -1
  55. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
  56. data/lib/samagotchi/version.rb +1 -1
  57. data/lib/samagotchi/web/app.rb +10 -2
  58. data/lib/samagotchi/web/peer_routes.rb +5 -2
  59. data/lib/samagotchi/web/session_access_routes.rb +5 -2
  60. data/lib/samagotchi/web/session_control.rb +6 -1
  61. data/lib/samagotchi/web/session_create.rb +1 -1
  62. data/lib/samagotchi/worker_boot.rb +28 -6
  63. metadata +3 -1
@@ -19,9 +19,9 @@ SessionManager boundaries are in [Architecture](../architecture.md) and [Engine
19
19
  | `owner.lock` and `leaving_at` | `OwnerLock.acquire` creates the file, takes its exclusive flock, and writes `{pid, kind, started_at}`; `OwnerLock#mark_leaving` adds `leaving_at` and `leave_wait`; `#release` unlocks and closes but leaves the file. Deletion removes it with the session directory. | `OwnerLock.owner` probes the kernel flock, then reads the record. The kernel releases the lock on process death; the record alone does not establish ownership. Presence and `WorkerLink` decisions probe it; hub presence refreshes poll it. `leaving_at` marks a worker that closed its Bridge but still holds the lock for leave work. |
20
20
  | `bridge.json` | `Bridge#write_sidecar` publishes the worker endpoint; `Bridge#remove_sidecar` removes its own sidecar when stopping. A crash can leave it behind. | `WorkerLink.resolve` combines the sidecar with `owner.lock` and pid; a sidecar alone is stale. Readers waiting for a worker poll `WorkerLink.resolve`; a stale unlink rechecks the owner and removes only the unchanged file. Live clients see worker events over Bridge SSE. |
21
21
  | In-memory exit claim | `Worker#exit_request` uses `WorkerExitClaim#claim`, `#accept`, `#release` and `#forget`; `#stopped!` records the last stop-all. It dies with that Worker. | Only that worker's threads read it; the Bridge's `/exit` and the loop coordinate through the Engine event lock, not a cross-process file. |
22
- | `stopped` | `SessionManager#stop_session` calls `Session.mark_stopped`; `SpawnPart#resume` and delivery's `#start_for_send` clear it with `Session.clear_stopped` when starting for a person. Session-directory deletion removes it. | `WorkerPresence`, the worker startup/signal paths, and `Session#load` check the marker. It is read from disk on a decision (and on the hub refresh); no worker event is required. Its timestamp is compared with `starting.at` so a stop cancels an older start, not a later resume. |
22
+ | `stopped` | `SessionManager#stop_session` calls `Session.mark_stopped` under the session's transition lock (kind `stop`, "Stops, archives and the ring wake"); `SpawnPart#resume` and delivery's `#start_for_send` clear it with `Session.clear_stopped` when starting for a person. Session-directory deletion removes it. | `WorkerPresence`, the worker startup/signal paths, and `Session#load` check the marker. It is read from disk on a decision (and on the hub refresh); no worker event is required. Its timestamp is compared with `starting.at` so a stop cancels an older start, not a later resume. |
23
23
  | `archived` | `ArchiveStore.archive` is called by `SessionManager#archive_session`; `ArchiveStore.unarchive` is called by `#unarchive_session`, `SessionContinue#undo_failed_continue`, and the chain-delete undo/revival paths. Session-directory deletion removes it. | Lists, chain resolution, delivery, schedules, retention, and `WorkerPresence` call `ArchiveStore.archived?` against the file; changes are seen by the next read or list refresh. |
24
- | `continue.lock` and `continue.json` | `SessionContinue#with_lock` takes the exclusive lock for a continue; `ContinueIntent#hold` takes a nonblocking shared lock for readers that must exclude a continue. `ContinueIntent#write` records the successor before archive and updates the archived ids after; failed-continue cleanup removes the intent. The lock file remains until session-directory deletion. | Followed sends, schedules, chain deletion, and `ContinueIntent.in_flight` inspect the lock and intent. flock coordinates the live transition; readers reload the JSON. An intent without a held lock is a crashed continue, not an active one. |
24
+ | `continue.lock` and `continue.json` | `SessionTransition` is the only code that flocks `continue.lock`: a continue takes it exclusively after its recap, for the previous link and the new link's reserved id at once (`SessionContinue#with_lock`: `exclusive`, `LOCK_WAIT` 10 s a try, then `Refused :transition_busy`), a user's link or chain delete takes it exclusively for its whole run (`SessionChain::Delete`, kind `delete`, `LOCK_WAIT` 10 s), the schedule prune, the retention sweep and a leaving worker's empty or scratch delete try it once (kind `delete`), a stop takes it for its mark and TERM (kind `stop`, `STOP_LOCK_WAIT` 10 s; a worker stopping its delegates tries once), an archive for its task stop and markers (kind `archive`, `ARCHIVE_LOCK_WAIT` 10 s), a ring's wake holds it shared across its check and resume, crash repair tries it once (`try_exclusive`), an exclusive holder records its kind in the file while it holds it (`holder`, which `Busy.held` reads), arrivals and readers that must exclude a continue take a nonblocking shared hold (`shared`: each `WakePolicy` row's `hold`, taken in the seats), and previews probe it (`held?`). `ContinueIntent#write` records the successor before archive and updates the archived ids after; failed-continue cleanup removes the intent. The lock file remains until session-directory deletion; a continue that ends before its new link is saved removes that link's reserved folder, and the previous link's lock when the link was deleted meanwhile (`SessionTransition.forget`). | Every `:shared` delivery (sends, every turn sender, pushes, peer says), peer pairs, context adds/removes/pushes, invites, schedules, the delete preview, `SessionLifecycle.read` (`transitioning`) and `ContinueIntent.in_flight` (through `SessionChain::Successor`) inspect the lock and intent. flock coordinates the live transition; readers reload the JSON. An intent without a held lock is a crashed continue, not an active one; so is one whose successor is neither saved nor held (a continue holds both), and one under another kind's hold (a delete, a stop, an archive: `SessionTransition.other_than_continue?`). |
25
25
  | `parent.json` | `Session#reparent` writes the override when `ChildMove` moves a child. Session-directory deletion removes it. | `ChildReports` resolves the current parent when routing a ring; the worker reads the override when loading its parent. Readers reload the file; it is not an event stream. |
26
26
  | `reminders.json` | `Worker::LeavePart#carry_reminders` writes it for an asked restart, or removes it when stopped/no successor; stop, archive, and a stopped restart also remove it. `ReminderCarry#take` reads and removes it at worker start. | Only the next worker restores it into its Engine. It is a one-shot hand-off, not polled state; the session id, 120-second window, and stop marker are checked before restore. |
27
27
  | Pending `input/`, `children/`, and `notes/` | `SessionInbox.write_input`, `#write_ring`, and `#write_note` write the files. A worker claims input and notes by atomic rename to `.processing`, then removes or settles them; rings are consumed by `ChildReports`. Session-directory deletion removes all three. | The worker checks input around turns and on its idle loop; rings and notes are checked by worker wake/absorption paths. The file is the durable queue; Bridge SSE can announce a live turn, but the worker's file scan is the fallback and recovery path. |
@@ -59,7 +59,8 @@ These are reader labels, not stored values:
59
59
  The compound states exist because the deciders disagree on precedence: owner first (WakePolicy, resume, archive,
60
60
  delete) or stop first (a schedule's busy check, a terminal's attach); WakePolicy reads archived before starting. Facts
61
61
  that are not states: `continuing` (`continue.json` names a successor and `continue.lock` is held: only a send's and a
62
- schedule's link resolution reads it), a stale sidecar (`WorkerLink::Stale`: a crashed worker's leftover, reads as
62
+ schedule's link resolution reads it), `transitioning` (`continue.lock` held by anyone: `SessionTransition.held?`;
63
+ any state can be held, so it is a fact beside the state, and `SessionLifecycle::TRANSITIONING` holds its cells), a stale sidecar (`WorkerLink::Stale`: a crashed worker's leftover, reads as
63
64
  the no-owner state), and the other crashed leftovers (a checkpoint, a `.processing` file, an unlocked continue intent).
64
65
  `SessionLifecycle::CANONICAL` holds the fewest facts per state, each deriving back to it; `stopping` is a worker whose
65
66
  Bridge answers, with a stop, and `stopped_starting` a start marked before the stop.
@@ -86,6 +87,14 @@ state. A cell is `:go`, `:wait` or the decider's own refusal reason; `SessionLif
86
87
  back); a ring, a push and a fresh verdict are refused before their write as for a gone parent (a push records to
87
88
  its source's folder as for a gone session), and `wake_for_report` answers `:deleting`; a leave's respawn drops
88
89
  (`why: deleting`).
90
+ - **While a transition holds the lock** (`SessionLifecycle::TRANSITIONING`, read by `allowed(op, state,
91
+ transitioning: true)`): every delivery whose row holds `:shared` is `:busy` in every state (refused before its own
92
+ checks, nothing written; `:continued` instead when the holder is a continue naming its new link, a resolution fact
93
+ as `continuing` is); a `:none` row answers its state's cell. Generated from the rows' `hold`, plus
94
+ `TAKES_LOCK`: a stop takes the lock itself (`stop_session`, exclusive: `:busy`, nothing marked) and so does the
95
+ ring's column, which is a ring's wake (`wake_for_report`, shared: `:busy`, the ring left on disk; the ring's own
96
+ write, a `:none` row, still goes in). The other literal operations read none (archive's column is `archivable!`,
97
+ its checks; `archive_session` takes the lock after them).
89
98
  - **Literal columns** (`LITERAL`): resume (`resume_session`), restart (`restart_session`: `stop_marked` in every
90
99
  state with the stop marked: stopping, stopped, stopped_starting), stop (`stop_session`),
91
100
  archive (`archivable!`), delete (`release_for_delete!`, without a stop), sched_chain and sched_same
@@ -127,7 +136,10 @@ SIGSTOP, features and Bridge reply do. The other deciders don't read `ALLOWED` y
127
136
  combination as real files: owner/link pairs (none, stale sidecar, tui, tui with a stale sidecar, deleting, a worker
128
137
  Live, Starting, stuck, Leaving, Skew, an owner of unknown kind) × stop × starting × archived, one absent session, a
129
138
  continue in flight on each archived no-owner row, and a start marked after the stop on each no-owner row with both
130
- (the order is a row fact: `StartingMarker.claim` takes over only a start at or before `stopped_at`). The owner lock's
139
+ (the order is a row fact: `StartingMarker.claim` takes over only a start at or before `stopped_at`), and four rows
140
+ (idle, archived, stopped, a live worker's) with the transition lock held for real by another thread
141
+ (`transitioning`): their deliveries go through the seats (`SessionInbox.deliver`, `deliver_turn`) and must answer
142
+ `TRANSITIONING`'s busy for a `:shared` row, the state's cell for a `:none` one. The owner lock's
131
143
  holder, `ContinueIntent.in_flight`, `Process.spawn`, TERM/CONT, the Bridge and the restart fallback are stubbed; no
132
144
  process starts. Each row checks that `SessionLifecycle.read` names the state its facts derive, then drives every
133
145
  operation (one driver each, every private reach in that one file) and projects its answer into a cell. A row the
@@ -149,9 +161,11 @@ of the state), not what WakePolicy does; the literal columns and `SHOWN` are gol
149
161
  ## Locks and crash repair
150
162
 
151
163
  `owner.lock` is the single-owner boundary for an Engine; its flock is released by process death. A starting claim uses
152
- an atomic link rather than a long-held lock. A continue holds its exclusive `continue.lock` across the transition;
153
- shared holds protect followed sends and chain operations. When chain deletion nests locks, it takes the continue hold
154
- before the `owner.lock` deletion locks. Inside a worker, the lock order is Engine event lock, then
164
+ an atomic link rather than a long-held lock. A continue holds its exclusive `continue.lock` (and its new link's) from after the recap to the end;
165
+ a user's delete holds them exclusively for its whole run, and the prune, the retention sweep and a leaving worker's
166
+ delete for each removal ("Deletes, the prune and retention"); a stop holds it exclusively for its mark and TERM, an
167
+ archive for its task stop and markers, and a ring's wake shared across its check and resume ("Stops, archives and
168
+ the ring wake"); shared holds protect every `:shared` arrival (the seats, "Arrivals declare their hold"). Every delete takes its transition locks before the `owner.lock` deletion locks. Inside a worker, the lock order is Engine event lock, then
155
169
  `WorkerExitClaim`'s mutex. Session saves serialize through their per-session save lock. These locks protect separate
156
170
  facts; there is no global lifecycle lock or atomic snapshot across all markers.
157
171
 
@@ -160,19 +174,196 @@ worker; under its owner lock, startup settles a turn checkpoint and stale runnin
160
174
  dead turn. Interrupted continue intents are repaired separately by session listing/the web hub or before another
161
175
  continue (`SessionContinue.repair_interrupted`), not by worker startup.
162
176
 
177
+ ### The transition lock (`SessionTransition`)
178
+
179
+ `continue.lock` is a session's transition lock, and `SessionTransition` (`session_transition.rb`) is the only code
180
+ in lib that flocks or probes it (`spec/session_transition_lock_spec.rb`). The file keeps its name: an older chi's
181
+ continue, followed send and delete honour it.
182
+
183
+ - `exclusive(ids, kind:, state_dir:, deadline:)` takes each id's lock in sorted order, creating a missing folder;
184
+ `deadline: nil` blocks (no caller since the continue took a deadline), seconds retry until then (`0`: one try)
185
+ and raise `SessionTransition::Busy`, which owns its refusal text and names the id. Ids taken before a busy one are
186
+ let go. While it holds a lock it writes its `kind` into the file (cleared on release, never from a lock another
187
+ holds; `try_exclusive` too).
188
+ - `holder(id, state_dir:)` is that kind (`:continue`, `:delete`), read only behind a failed shared try: a shared
189
+ holder (a send), an older chi's continue, a raw flock and a crash's leftover name none. `Busy.held(id, state_dir:)`
190
+ is the one place that says who holds a lock: a delete is "being deleted" whatever else is on disk; else a continue
191
+ in flight (`ContinueIntent.in_flight`) is "being continued as <new id>" (`SessionChain::Continued`'s words), a
192
+ continue before its intent "being continued", anything else "busy with another change" (each "; try again in a
193
+ moment"). A stop is "being stopped" and an archive "being archived", said as a delete is: a non-continue holder
194
+ (`other_than_continue?`) names no new link, and `in_flight` reads no continue under it, whatever intent a crashed
195
+ continue left. `exclusive`'s own Busy, the seats' busy refusal (`deliver_turn`), the continue's `transition_busy`
196
+ (its detail, which `--remote` reads back), `ContextSources.changing`, `Participants.invite`, the delete's refusal
197
+ (`DeleteRefused.held`) and the delete preview take their words from it; a delete holding the lock makes the seats
198
+ name no successor (`Hold.under`). The peer say/pair and the context push answer a status, `:busy`, whose words
199
+ are the plain ones.
200
+ - `try_exclusive(id, …)` is one try that creates no folder, crash repair's (`repair_intent`): a lock this thread
201
+ holds counts as taken, so a repair reached inside a continue never undoes it.
202
+ - `shared(id, state_dir:)` is a non-blocking shared hold (an invalid id is `false`; the lock file is created, the
203
+ folder only for a session whose file is there: a deleted session's folder is never made again, nothing is held and
204
+ the caller's own gone refusal follows; a lock file it made for a session gone by the block's end is taken back),
205
+ around every arrival's check and write (the seats, below), a peer say's, a peer pair's and a context push's
206
+ continued check and write (`SessionAccess::Local#moved_on`), a context add or remove (`ContextSources.changing`),
207
+ and an invite (`Participants.invite`). A caller that can't write the session folder
208
+ (a peer's chi behind the mailbox door) holds an existing lock file read-only, and holds nothing when there is none
209
+ (no transition holds a file that isn't there; one starting after it is not held off).
210
+ - `held?(id, state_dir:)` is a kernel probe (`in_flight`, `DeletePreview`, `holder`): it creates nothing and ignores the
211
+ registry, so it answers the same inside this thread's own hold.
212
+ - `forget(id, state_dir:)` takes back the lock file `exclusive` left for a session with no session file (one try
213
+ EX|NB; held, or this thread's, it stays) and its folder when that empties it.
214
+
215
+ **Order:** transition locks (several ids sorted) before any `owner.lock`. A taker that holds an `owner.lock` (a worker:
216
+ a delegate follow-up, an ask, a plugin's send, a ring, its own objectives note) only ever takes a transition lock
217
+ non-blocking: shared, or, for a stop of its delegates (`HoldStop#stop_delegates`, `Plugin::Sessions#stop`:
218
+ `stop_session(lock_wait: 0)`), exclusive in one try; so no owner-then-transition path waits. **Token per thread:** an exclusive hold is
219
+ the current thread's (`Thread#thread_variable_*`, not per process: chi web is multi-threaded, and not fiber-local);
220
+ that thread passes through `exclusive` and `shared` of a held id without touching the file (a second flock on the
221
+ held descriptor, or a dup, would convert it). Work handed to another thread doesn't carry the hold: it takes it again
222
+ or is refused. Before the registry, a shared hold inside a continue's own lock was refused; nothing a continue runs
223
+ under its lock takes one of its own id or reaches `follow_guard`, `Delete.link` or `Delete.chain`
224
+ (`spec/session_continue_spec.rb` records any), but the undo's ring wake (`wake_for_rings` → `wake_for_report`), whose
225
+ shared hold passes through on the token by design. **No fork while held:** a forked child keeps the flock; workers
226
+ start through `Process.spawn`, whose descriptors are close-on-exec.
227
+
228
+ **A continue's hold** (`SessionContinue.run` → `continue_once`, `spec/session_continue_spec.rb` "the transition
229
+ lock"). Unlocked: the refusals known before anything changes (`check!`, `archivable!`) and the recap wait (up to
230
+ `RECAP_WAIT` 30 s), so a send during the wait goes in (the archive then refuses it as before) and a delete may take the
231
+ link (its exclusive hold then makes the continue's wait for the lock end busy, "being deleted"). Then one `exclusive([previous, reserved successor id])`, sorted, `LOCK_WAIT` (10 s) a try: under it the previous
232
+ link is loaded and `check!`ed again (continued meanwhile → `Refused :continued`; deleted → not found; the delegate
233
+ plan made again), then the intent, the archive (`archive_session` → `stop_session`, nested), the new link's spawn and
234
+ carry (notes, peers, context, objectives, `ChildMove` notes and rings), or the undo (`return_rings`,
235
+ `NoteMove.give_back`, unarchive, `wake_for_rings` → `wake_for_report` → `resume_session`, `discard_failed_link`'s
236
+ stop and delete of the new link). All of it runs on the continue's thread under its token: a shared hold or a nested
237
+ exclusive of either id passes through, so no step refuses itself (`exclusive` skips held ids). Busy: a plain id is
238
+ refused `:transition_busy` (`SessionTransition::Busy.held`'s words naming the holder, a 409 of that name from chi
239
+ web; a schedule's chain firing counts it a skip, `Schedules::SKIP_REASONS`); a `last:<id>` continue with tries left
240
+ resolves again and waits again, so a second firing of the same chain follows the first one's new link. Any sender's turn
241
+ that meets the hold is refused naming the successor from the intent (`follow_guard`, "Arrivals declare their hold"
242
+ below: `SessionChain::Successor.in_flight`), else `transition_busy`; so are a peer say, a peer pair and a context push into the link's own source
243
+ (`:continued` with `in_flight`), or `:busy` in `SessionTransition::Busy`'s words when the holder names none. Who
244
+ continues a link is one answer, `SessionChain::Successor.of(id, state_dir:, in_flight:)` (`via` `:next`, `:anchor`
245
+ or `:in_flight`), and the refusal one wording, `SessionChain::Continued` ("session X was continued as Y", "is being
246
+ continued as" in flight), each site adding only what to do next. The new link's worker starts inside the hold, in its own process: its boot writes into its
247
+ own session (`ChildMove.adopt`, `NoteMove.adoption`, its objectives notes) take no transition hold, and must not (they
248
+ would be refused by the continue's exclusive). The reserved folder (`exclusive` makes it) goes whatever way a continue
249
+ ends before the new link is saved, with any note left there given back to the previous link; crash repair drops a
250
+ crashed continue's the same way. `Schedules::FIRING_TIMEOUT` (70 s) covers the recap, a lock wait per try and the
251
+ archive.
252
+
253
+ ### Deletes, the prune and retention
254
+
255
+ - **A user's delete** (`SessionChain::Delete.link` / `.chain`, from `delete_session` with the chain keywords and
256
+ `delete_chain`: the CLI, chi web, `/exit --delete`) takes `exclusive(ids, kind: :delete, deadline:)` of every
257
+ session it takes or brings back (a link: it, the previous link it would revive, its delegates that go; a chain:
258
+ every link and their delegates), only ids still on disk (a gone one's folder is never made again), sorted, within
259
+ `LOCK_WAIT` (10 s) from the start. The plan is made (a chain's checks run) again under them; ids it adds then (late)
260
+ are a nested `exclusive` with the time left (held ids pass through). The stop (`release_for_delete!` →
261
+ `stop_session`), the task stop, the owner locks (`locked`, kind deleting), the removal, the undo
262
+ (`undo_deleted_link`) and the revival all run under the token. A lock not taken in time is
263
+ `DeleteRefused.held(busy)`: `:continuing` when a continue holds it, else `:transition_held` (chi web's 409 of that
264
+ name, which `--remote` reads back as `DeleteRefused`), Busy's words either way. A continue holding one of the ids
265
+ when the delete looks refuses it at once (`:continuing`, no wait: its end would change what the delete takes), and a
266
+ link continued while the delete waited for the locks is refused `:continuing` naming the new link rather than
267
+ deleted as a middle link. `refuse_continuing:` no longer
268
+ changes anything inside: either keyword takes `Delete.link`, which always refuses a held lock.
269
+ - **The schedule prune** (`Schedules::PrunePart`) takes `exclusive([link, next link], kind: :delete, deadline: 0)` per
270
+ link, oldest first, and checks the link's hold (`prune_hold`: archived, a protector, a worker, an open descendant)
271
+ before and again under it. Busy or a hold stops the prune below the link: never skipped past. The entry records it
272
+ (`extra["prune_waiting"]`, `PrunePart.waiting`; `chi schedule list`/`show`), the log says `prune_waiting` once per
273
+ link, and every tick tries a waiting entry again (`prunes_due`). The deletes inside are plain (`delete_ids`).
274
+ - **The retention sweep** (`SessionRetention.apply`, `prune_orphan_dirs`) and **a leaving worker's delete**
275
+ (`WorkerBoot#discard_left_session`, `#delete_left_scratch`, after the worker let go of its owner lock) take
276
+ `exclusive([id], kind: :delete, deadline: 0)` around the removal: held → kept (the sweep's `kept`; the worker's
277
+ `kept_session` / `scratch_kept`), and a later sweep takes it.
278
+ - The automatic deletes inside a transition (a continue's `discard_failed_link`) and `delete_resolved` /
279
+ `remove_files` themselves take no transition lock: the callers above hold it, so a continue's undo never refuses
280
+ itself.
281
+
282
+ ### Stops, archives and the ring wake
283
+
284
+ - **A stop** (`SessionManager.stop_session`) takes `exclusive([id], kind: :stop, deadline: lock_wait)`
285
+ (`STOP_LOCK_WAIT` 10 s) for the owner read, the stop marker, the reminders hand-off and the TERM/CONT; the turn
286
+ cancel before it (a Bridge call and up to `STOP_CANCEL_WAIT`) and the owner wait after it (`wait:`) run unheld. Only
287
+ a saved session's lock is taken (a gone one's folder is never made again; `Session.mark_stopped`'s not found
288
+ follows). A hold this thread has passes through: a continue's archive (`archive_session` → `stop_session`), its
289
+ `discard_failed_link`, a delete's `release_for_delete!`; a plain delete with a stop (no chain keywords, a scratch's
290
+ end: `delete_resolved` takes no transition lock) turns a held one into its own refusal (`stop_held` →
291
+ `DeleteRefused.held`). Not taken in time → `SessionTransition::Busy`,
292
+ nothing marked: chi web's 409 `stop_held` (`kind`, `successor_id`: `--remote` builds the same Busy), `chi sessions
293
+ stop` prints it and exits 1. A worker stopping its delegates (the exit ask's stop-all, a plugin's
294
+ `ctx.sessions.stop`) tries once (`lock_wait: 0`): it holds its own `owner.lock`; a held child is left running
295
+ (logged / the plugin's `Error`, Busy's words).
296
+ - **An archive** (`archive_session`) stops the tree's live workers first (each stop its own hold, as above), then
297
+ takes `exclusive(tree on disk, kind: :archive, deadline: ARCHIVE_LOCK_WAIT)` (10 s) for the background-task stop
298
+ and the archived markers; a delegate its stopping worker discarded gets no folder again. Inside a continue the
299
+ previous link passes through and its delegates are taken. A lock not taken in time, its own or a stop's, is
300
+ `ArchiveRefused.held` (`:transition_held`, Busy's words; chi web's 409 `busy` with that reason, `--remote` keeps the
301
+ words). Its checks (`archivable!`) come before any lock, as before.
302
+ - **A ring's wake** (`wake_for_report`: `SessionInbox.wake(:ring, hold: :shared)`, on the resolved link) holds the
303
+ lock shared across the decide and the resume, so a stop lands before the check or after the start (its marker then
304
+ stays and the new worker exits on it, or the TERM reaches it): never between them. A transition holding it now →
305
+ `:busy` (`:continued` with a continue's new link), the ring left on disk. The ring's own write and its first wake
306
+ (`ChildReports.ring` → `SessionInbox.deliver(:ring)`, a `:none` row, its start on another thread) hold none:
307
+ still open (Open windows).
308
+ - **A restart** stays outside the lock (D3): it spans two processes (the old worker's leave, the new one's spawn).
309
+ Open windows has its row.
310
+
311
+ ### Arrivals declare their hold
312
+
313
+ Every `SessionInbox::WakePolicy::ROWS` row names `hold:` (a required keyword: a row without it doesn't load), pinned
314
+ by `spec/session_lifecycle/allowed_spec.rb`. The hold is taken once, in the three seats, by `SessionInbox::Hold.under`:
315
+ `Dispatch.deliver` (around the refusal check, the write and the wake), `Dispatch.wake` (around the wake) and
316
+ `SessionManager.deliver_turn` (`follow_guard`, around all of the delivery: the resume, the Bridge's wait and post, the
317
+ file and the second liveness check). Not in `resume_session` / `start_for_inbox`: a continue's undo reaches them on its
318
+ own token. A seat met by a held lock refuses, nothing written: a continue naming its new link (`Successor.in_flight`,
319
+ else a saved one) → `:continued` / 409 `link_continued` with `successor_id`; else `:busy` / 409 `transition_busy`
320
+ (`DeliverResult::BUSY`, `SessionTransition::Busy`'s words). chi send, an ask and the Exchange under them retry busy as
321
+ they follow a continue (`Exchange#relocate busy:`: the same link again, within `CONTINUING_WAIT`); a same-mode schedule
322
+ firing counts it a skip; the web composer, a delegate follow-up, a plugin's send and a participant's turn report it.
323
+
324
+ | Row / arrival | Hold | Why |
325
+ |---|---|---|
326
+ | send, push, peer, schedule, plugin, delegate, participant, agent, automatic | `:shared` | A turn or a push landing in a link being continued would be un-archived beside the new link or never read. |
327
+ | note (chi note, `send_note`, a broadcast, objectives) | `:none` | `NoteMove` moves a note written during a continue, the new link adopts one after (`NoteMove.adoption`), a failed continue gives them back; the new link's boot writes its own inside the continue's hold. |
328
+ | ring | `:none` | `ChildReports` rings once, no retry; `ChildMove` moves rings (`return_rings` gives them back); a continue's undo wakes for them on its own token. |
329
+ | fresh_verdict | `:none` | `VerifyFresh.forward` writes once, no retry; the verdict is for the card in the parent it names, continued or not; it starts no worker. |
330
+ | leave (respawn) | `:none` | After the worker's own release, spanning two processes (outside the lock); a refusal would drop a restart or queued input's start with no retry; a continue stops the worker first and the stop wins. |
331
+ | context add / remove (`Local#context_add`/`#context_remove`, chi web's URL add and detach) | shared | `ContextSources.changing`: a continue copies the session's own sources. Refused `ContextSources::Held` (423 `context_busy`). |
332
+ | context push, peer say, peer pair | shared | Phase 1 (`Local#moved_on`); the say's delivery seat holds again (shared beside shared). |
333
+ | invite (`Participants.invite`) | shared | A continue archives the link and ends its grants; refused `Grants::Invalid` with Busy's words. |
334
+ | revoke, peer unpair | none | Removals: a continue ends every grant at the archive, and the new link's pairing is the peer's to make (`Plugin::Peer#tell_moved`); refusing them would only make the owner retry. |
335
+ | `Local#answer` / `#dismiss`, cancel (cut), the attached TUI's `post_turn`, `Bridge#enqueue_turn` (the input fallback) | worker-side | Bridge-only: the live worker's own session (a continue's archive stops that worker and `archivable!` refuses it busy or queued). Over `--remote` the TUI's turn is chi web's turn route, `deliver_turn`'s seat. |
336
+ | The new link's boot (`ChildMove.adopt`, `NoteMove.adoption`, its objectives notes) | worker-side, none | Runs inside its creator's exclusive hold of the new link: a hold there would be refused until the continue ends. |
337
+
338
+ The raw `SessionInbox.write_*` sites are pinned with their hold in `spec/session_lifecycle_writers_spec.rb`. An older
339
+ chi's (0.190.x) worker or chi web takes no hold: its arrivals go in as before, and the tolerance paths stay
340
+ (`archivable!` refusing queued/busy, `NoteMove`, `ChildMove`'s movers).
341
+
163
342
  ## Open windows
164
343
 
165
344
  These are documented race boundaries, not promises of an atomic lifecycle transition:
166
345
 
167
346
  | Window | Code comment / current behavior | Pin |
168
347
  |---|---|---|
169
- | A ring wake decides to start, then a stop lands before resume. | `SpawnPart#wake_for_report` (`session_manager/spawn.rb`): `resume_session` can clear that stop and start; the check and resume are not one transaction. | Unit spec: stop in the start callback and assert the worker is started and the marker cleared. |
170
- | Empty-session discard races a delivery (closed for turns and notes). | `WorkerBoot#discard_left_session` (`worker_boot.rb`) checks emptiness after releasing the worker lock and again under the deletion lock (`delete_session`'s `still:`): a send or note that went in before the lock keeps the session. One that reads the session during the lock is refused (`DeliverResult::DELETING`, a note as gone); one that read it before the lock and wrote after it reads it again after its write and takes its file back (`SessionInbox.withdraw`), so no turn or note is reported delivered and then removed. No new lock: the write-then-reread and the recheck under the existing deletion lock close it. What remains: a ring or fresh verdict that read the session before the lock and wrote after it is not taken back: it goes with the directory, or stays for the next worker if the delete gives up; neither is reported to a person. | `run_session_loop_spec.rb`: a send at deletion-lock acquisition is refused; one just before the lock keeps the session. `delivery_spec.rb` / `note_delivery_spec.rb`: the lock (or the removal) between the check and the write, deterministic through the bridge / writer seam. |
348
+ | A ring wake decides to start, then a stop lands before resume (closed). | Before: `SpawnPart#wake_for_report` (`session_manager/spawn.rb`) checked, then `resume_session` cleared a stop that landed meanwhile and started the worker. Now the wake holds the session's transition lock shared across the check and the resume and the stop takes it exclusively for its mark and TERM: the stop comes after the start, whole (its marker stays), or the wake meets the stop's hold and leaves the ring on disk (`:busy`). | `spec/session_manager/spawn_spec.rb` ".wake_for_report" (a stop from another thread in the start callback is held off and its marker stays; a held lock answers `:busy`, nothing started). |
349
+ | A ring's own delivery wakes the parent while a stop lands. | `ChildReports.ring` → `SessionInbox.deliver(:ring)` (a `:none` row: its write must never be refused) decides, then starts the parent on another thread (`ChildReports#starter`, `start_for_inbox`): the hold of `wake_for_report` does not cover it, so a stop between the decide and the start can be cleared by it as before. | No pin (declared); a shared hold around the start alone, on the starter's thread, would close it. |
350
+ | A restart outside the transition lock. | `restart_session` asks the worker to leave and its successor to start (or the fallback TERMs and resumes); it spans two processes, so it takes no transition lock (D3): a continue, a delete, a stop or an archive may interleave with it, each with its own checks (the restart precheck reads `SessionLifecycle`). | No pin (declared). |
351
+ | A stop's turn cancel and owner wait are unheld. | `stop_session` cancels a running turn (Bridge, up to `STOP_CANCEL_WAIT`) before it takes the lock and waits for the owner after it lets go: a send in either may go in (and a new worker start after the wait, as before). A stop refused busy after the cancel has ended that turn. | `spec/session_manager/lifecycle_spec.rb` "under the transition lock" (held for the mark and TERM, not the owner wait). |
352
+ | Empty-session discard races a delivery (closed for turns and notes). | `WorkerBoot#discard_left_session` (`worker_boot.rb`) checks emptiness after releasing the worker lock and again under the deletion lock (`delete_session`'s `still:`): a send or note that went in before the lock keeps the session. One that reads the session during the lock is refused (`DeliverResult::DELETING`, a note as gone); one that read it before the lock and wrote after it reads it again after its write and takes its file back (`SessionInbox.withdraw`), so no turn or note is reported delivered and then removed. No new lock for that: the write-then-reread and the recheck under the existing deletion lock close it (the discard also tries the session's transition lock once, kind `delete`: a send or a transition holding it keeps the session). What remains: a ring or fresh verdict that read the session before the lock and wrote after it is not taken back: it goes with the directory, or stays for the next worker if the delete gives up; neither is reported to a person. | `run_session_loop_spec.rb`: a send at deletion-lock acquisition is refused; one just before the lock keeps the session. `delivery_spec.rb` / `note_delivery_spec.rb`: the lock (or the removal) between the check and the write, deterministic through the bridge / writer seam. |
171
353
  | Stop reads an owner pid, then sends TERM. | `LifecyclePart#stop_session` (`session_manager/lifecycle.rb`): the pid can be reused between the owner read and `Process.kill`; protecting that requires a real pid-reuse scenario. | No unit pin: pid reuse/process signaling is not reliably representable without real processes. |
172
354
  | Saved `status` is used as a lifecycle hint. | `LifecyclePart#restart_fallback_holds` and `RemovalPart#archive_refusal` (`session_manager/lifecycle.rb`, `session_manager/removal.rb`) use saved `running` status as a busy hint; status is not a lock or proof that a worker is alive. | Unit spec: pin fallback refusal from saved status without a live Bridge. |
173
355
  | Stop/start ordering depends on wall-clock `at`. | `SpawnPart#claim_start` and `StartingMarker#claim` (`session_manager/spawn.rb`, `starting_marker.rb`) compare `starting.at` with `Session.stopped_at`; same-time starts are treated as before the stop. Clock adjustments across processes are not serialized. | Unit spec: pin the inclusive `at <= over_before` takeover boundary. |
174
356
  | A delete stops a session's background tasks. | `RemovalPart#remove_session_files` (`session_manager/removal.rb`): with `running_tasks:` a stop, `RunningTasks.stop!` runs under the DELETING owner lock, after the `StartingMarker.fresh?` re-check and `still`, for up to ~4 s; meanwhile a send, a schedule or a resume is refused as deleting. With `REFUSE` the same seat lists the final file again and refuses a task started since the op's check (`worker_stopped` set when the op stopped the worker; `stopped_worker` names whose). `SessionChain::Delete` lists a whole batch (a link and its D7 delegates) once under the batch's locks, before any of it goes (`refuse_tasks!`), and its removals then skip the per-id re-check (`RunningTasks.after_check`), so no refusal comes after part of the batch went. A chain with `--stop-tasks` stops link by link inside each `locked(batch)`, serially: up to ~4.5 s per link, N links N times that, with that batch's locks held; a link refused part-way keeps its tasks running. A stop that outlives the join's deadline (`unconfirmed`) is reported as not stopped and may still finish after the files are gone (its thread goes on writing the record under the session's working directory, not the session dir). | `removal_spec.rb` (a worker's `OwnerLock.acquire` during the stop fails; the re-check refuses a new task), `session_chain/delete_spec.rb` (link by link, part-way; a task started after the up-front check keeps the link, its delegate and the chain whole). |
175
- | An archive stops a session's background tasks without a lock. | `RemovalPart#archive_session`: the stop runs after the tree's workers were stopped and waited for (`stop_session(wait:)`), before the marker; a worker that outlives the wait records the stop itself from the `stop_requested_by` flag on disk (`record_exit_status` / `correct_finished_record`). A task started after the listing isn't stopped: archive already refuses `busy` up front. | `removal_spec.rb` (the stop sees no owner left). |
357
+ | An archive stops a session's background tasks without a lock (closed for transitions and sends). | `RemovalPart#archive_session`: the stop runs after the tree's workers were stopped and waited for (`stop_session(wait:)`), before the marker; a worker that outlives the wait records the stop itself from the `stop_requested_by` flag on disk (`record_exit_status` / `correct_finished_record`). The task stop and the markers now run under the tree's transition locks (kind `archive`): a send, a continue or a delete doesn't interleave with them. Still open: a task started after the listing isn't stopped (archive refuses `busy` up front), and a send between the workers' stop and the archive's hold goes in (the archive then hides it, as before). | `removal_spec.rb` (the stop sees no owner left); `session_archive_spec.rb` "under the transition locks" (held as an archive for the markers, Busy → `transition_held`). |
358
+ | A peer say, peer pair or context push during a continue (closed, N1). | Before: `Local#peer_say` checked `continued_as` (archived with a saved successor) and posted through `SessionInbox.deliver(:peer)`, a pinned row that skips Head's in-flight look and `follow_guard`; between the archive and the new link's save a say went into the old mailbox after `PeerContinue` had read it, and was never read (its line said "it reads this if you reopen it"). Now each takes `SessionTransition.shared` across the continued check and the write (`Local#moved_on`), never across the Bridge feature probe or the receipt wait: a continue holding the lock refuses it `:continued` naming its new link (in flight), else `:busy`. A say that holds the lock first goes in and the continue's carry takes it. | `session_access_local_spec.rb` "while a transition holds the session's lock" (say, pair, push refused naming the successor, Busy without one, mailbox and source unchanged); `session_continue_spec.rb` "refuses a peer say during the transition" (a real continue, the say made between its archive and the new link's save). |
359
+ | A send between a continue's exclusive hold and its archive marker (closed). | Before: `follow_guard` held only for a followed send to an archived link, so a send (any sender) to a link a continue held but had not archived yet went in, and the archive then refused the continue as busy or queued. Now every `:shared` row's delivery takes the hold in its seat: refused naming the new link (`link_continued`), else `transition_busy`. | `spec/session_inbox/hold_spec.rb` (each sender busy under a really held lock; a send to a link not archived yet refused naming the new link); the matrix's transitioning rows. |
360
+ | A frozen worker's Bridge post under a send's hold. | `deliver_turn` holds the session's lock shared across the Bridge's post (up to `BridgeClient::READ_TIMEOUT` 30 s for a worker that doesn't answer); a continue of that session waits `LOCK_WAIT` per try and is refused `transition_busy`, and a stop or an archive waits its 10 s and is refused (`stop_held` / `transition_held`, "busy with another change"): stopping that frozen worker can take a second try. | No unit pin: a frozen worker's post is a timing case. |
361
+ | A delete or a send during a continue's recap. | `SessionContinue.continue_once` waits for the recap before it takes the transition lock: in that window a link delete and the delete preview no longer see a continue (`:continuing`): the delete may take the link's lock exclusively, and the continue's lock wait then ends busy ("being deleted"); a followed send to the link goes in. Under the lock the continue checks the link again: deleted → not found (its folder not brought back), continued by another → `Refused :continued`, a turn queued → the archive's refusal. | `session_continue_spec.rb` "the transition lock": the lock not held during the recap, a send then accepted, a link deleted or continued during the recap refused under the lock. |
362
+ | The new link's worker boots inside the continue's hold. | `SessionContinue.spawn_continuation` spawns it while the continue holds the new link's transition lock exclusively; its boot writes (`Worker#start` → `ChildMove.adopt`, `WorkerInbound` → `NoteMove.adoption`, its objectives notes through the note row, `:none`) take no transition hold. A hold added on that path would be refused until the continue ends (drop the new link's hold before the spawn instead). A send or a peer pair to the new link from outside is `transition_busy` until the continue lets go (milliseconds after the spawn): the senders retry. | `session_continue_spec.rb`: the boot's adoption on another thread makes no `SessionTransition` call, adopts the note, and a note through `NoteDelivery` goes in. |
363
+ | An unpair or a revoke during a continue. | `:none` (above): an unpair of the old link while the new worker's `tell_moved` re-pairs the peer may leave the peer paired with the new link. | No pin (declared). |
364
+ | Sends to a session a delete holds (open, by design). | `SessionChain::Delete` holds the transition locks from before its stop to the end: a send, a push, a peer say or a context change to the link, its previous link or a delegate that goes is refused `transition_busy` "being deleted" for the stop (up to the delete's wait, 10 s from the CLI), the task stop (about 4 s a link) and the removal; a chain delete holds every link's for the whole chain (N links × stop + tasks: minutes for a long chain with `--stop-tasks`). chi send and an ask retry within their wait; the web composer, a delegate, a plugin and a participant report it. Before, those sends went in and the delete stopped or removed them. | `session_chain/delete_spec.rb` "under the transition locks" (a send during the stop refused as being deleted, both locks the delete's). |
365
+ | A delete meets a send's or another change's hold (closed). | Before: `Delete.link`, `hold_all` took `SH\|NB`, so a continue was refused but another delete, a prune or the retention sweep could run beside it, and a send held nothing off. Now a user's delete waits up to `LOCK_WAIT` for the locks exclusively, then refuses (`continuing` / `transition_held`, the holder named); the prune, the sweep and a leaving worker's delete try once and keep the session. | `session_chain/delete_spec.rb` (a short shared hold waited out, a long one refused, a held delegate refuses), `session_retention_spec.rb`, `session_discardable_spec.rb` (held → kept). |
366
+ | A prune at a link it can't delete (closed: visible, never skipped). | Before: a link another enabled schedule protects (its anchor inside the chain, a stale head) was refused `prune_stopped` at every firing, after the link's delegates were already deleted, and the chain grew silently. Now the hold is checked before anything in the subtree goes and the prune stops there with a record (`prune_waiting`, list/show) and one log line; a held transition lock on the link or the next does the same, and every tick retries. Still open: the chain grows behind such a link until the other schedule is cancelled (re-linking the chain around it is held, D6). | `schedules_spec.rb` "a prune held at a link" (held link: kept with every later link, resumed next tick; next link held; the mid-chain schedule: one log, list/show lines, nothing deleted around it, gone after cancel). |
176
367
  | Several methods write or remove the same marker. | The definitions/callers in `session.rb`, `starting_marker.rb`, `archive_store.rb`, `bridge.rb`, and `reminder_carry.rb` are the source of truth; these APIs have multiple call sites. | `spec/session_lifecycle_writers_spec.rb` pins the call-site allow-lists; it does not claim their operations are one transaction. |
177
368
 
178
369
  ## Adding a transition
@@ -183,7 +374,17 @@ These are documented race boundaries, not promises of an atomic lifecycle transi
183
374
  3. Identify every decider that reads the fact and how it observes the change (lock probe, direct read, poll, or SSE).
184
375
  4. If the change touches an open window above, extend its pin in the corresponding unit spec; don't hide a race with a
185
376
  sleep or a process-wide integration test.
186
- 5. If the fact changes a state, update `SessionLifecycle::STATES`/`Reading.derive` and `CANONICAL`, then the
377
+ 5. A new arrival (a `WakePolicy` row, a delivery outside the seats) declares its hold: `hold: :shared` (the seat
378
+ takes it) or `hold: :none` with its reason; a raw `SessionInbox.write_*` site is listed with its hold in
379
+ `spec/session_lifecycle_writers_spec.rb`; one outside the inbox that changes what a transition carries takes
380
+ `SessionTransition.shared` around its check and write (see "Arrivals declare their hold").
381
+ 6. If the fact changes a state, update `SessionLifecycle::STATES`/`Reading.derive` and `CANONICAL`, then the
187
382
  `ALLOWED` cells (the literal ones by hand; the delivery ones follow `WakePolicy::ROWS`) and the matrix's rows; a
188
383
  decider that answers otherwise is a `KNOWN_DIFFERENCES` entry with its reason, never a silent cell change.
189
- 6. Recheck lock order, crash cleanup, and the existing file-format/compatibility specs before changing runtime behavior.
384
+ 7. A new multi-step transition takes `SessionTransition.exclusive` with its own `kind` (add its words to
385
+ `Busy::WORDS` so `Busy.held` names it; any kind but `continue` names no new link), with a deadline, before any
386
+ `owner.lock`; an automatic one tries once (`deadline: 0`) and leaves the session for later, and so does one a
387
+ worker runs (it holds an `owner.lock`). A literal operation that takes the lock gets its `TAKES_LOCK` cell.
388
+ 8. Recheck lock order, crash cleanup, and the existing file-format/compatibility specs before changing runtime behavior.
389
+ A transition that must exclude a continue or an arrival takes the transition lock through `SessionTransition`,
390
+ never its own flock of `continue.lock`.
data/docs/peers.md CHANGED
@@ -56,7 +56,9 @@ lost: it is a note, and the user's next message lets chi read it. The skills say
56
56
  `chi send` past it.
57
57
 
58
58
  A say is refused, with nothing written, for: no such session; a session being deleted; a continued one (`this
59
- session was continued as <id>: say it there`); not paired (`not paired: run the pair script`); an older worker
59
+ session was continued as <id>: say it there`); one being continued right now (`this session is being continued as
60
+ <id>: say it there in a moment`: its new link is not saved yet); one busy with another change (`this session is busy
61
+ with another change; try again in a moment`); not paired (`not paired: run the pair script`); an older worker
60
62
  (`restart the session`); 200 messages chi hasn't read yet.
61
63
 
62
64
  Through `--remote NAME` (or `CHI_REMOTE`) the remote's chi web does the say and the wait, and returns the line.
data/docs/plugins.md CHANGED
@@ -1529,7 +1529,8 @@ ctx.sessions.stop(id) # one of this session's own children
1529
1529
  - `send(id, text)` sends a user message to a session (an id or a unique
1530
1530
  prefix); it runs as a turn, and an idle session is woken. A session its
1531
1531
  owner stopped or archived is not: `Error` (`session_stopped` /
1532
- `session_archived`), nothing sent; nor one being deleted (`session_deleting`). It waits up to
1532
+ `session_archived`), nothing sent; nor one being deleted (`session_deleting`) or held by a transition right now
1533
+ (`link_continued`, `transition_busy`: try again in a moment). It waits up to
1533
1534
  5 s for the session's worker, so call it from an anytime command or a
1534
1535
  thread of your own, never from a tool or hook of a running turn. A
1535
1536
  session a `--non-interactive` one-shot has can't take it.
data/docs/scheduling.md CHANGED
@@ -173,8 +173,12 @@ are the refusals that mean "not this moment": `ArchiveRefused` with `busy`, `que
173
173
  or `offer`, or `busy_child`/`queued_child`/`offer_child` (a chain's own running
174
174
  delegate refuses the archive, since `archivable!` walks the whole descendant tree),
175
175
  `SessionContinue::Refused(:continued)` (a human continued it while we were
176
- resolving) and `:open_children`. So is an archived target that a continue is still
177
- making the next link of (`being continued as 7b2d41aa; retry at the next tick`): a
176
+ resolving), `:open_children` and `:transition_busy` (another continue of the
177
+ chain's end held its transition lock past the continue's 10 s on each of its tries), and a
178
+ same-mode turn its target's transition lock refused (`transition_busy`, or `link_continued` naming a
179
+ continue's new link: the turn's own hold, nothing written).
180
+ So is an archived target that a continue is still
181
+ making the next link of (`session 3f9a1c2e is being continued as 7b2d41aa-…; retry at the next tick`): a
178
182
  person's `chi send --new --continues` or web `continue →` archives the link before
179
183
  its successor is saved, and a tick in that moment sees an archived end
180
184
  (`ContinueIntent.in_flight`: the intent written, its lock held). It is checked before
@@ -277,8 +281,10 @@ is refused with exit 2, naming the matches.
277
281
  The tick's lock covers the file only, never the action: the clock advances before
278
282
  the work starts — for `every` and `weekday` to the next occurrence, for a `date`
279
283
  one-off to none — so a tick killed mid-continue cannot fire the same entry twice.
280
- If a later tick finds a firing older than 40 seconds without an accepted turn, it
281
- counts an `interrupted` failure. This 40-second limit covers only the firing
284
+ If a later tick finds a firing older than 70 seconds without an accepted turn, it
285
+ counts an `interrupted` failure (a chain firing's continue can wait 30 s for the
286
+ recap, 10 s for the transition lock on each of its three tries, 5 s for the
287
+ archive). This 70-second limit covers only the firing
282
288
  step, not an accepted turn or its pending question. The action may already have
283
289
  taken effect before the process died, so that occurrence is not replayed; a
284
290
  `date` one-off stays disabled with the failure visible, and recurring schedules
@@ -388,13 +394,31 @@ whole subtree only when every session in it is archived and no worker owns any o
388
394
  it, deepest child first and the link last, oldest link first. The delete itself is
389
395
  `SessionManager.delete_ids` (the prune keeps its own checks and log tags): each id
390
396
  through `delete_session`, and System 1's saved runs and the candidate log purged once
391
- per link's batch, not once per session. A subtree with a live
392
- worker or an unarchived descendant is left whole and retried at the next firing. Pruning a link alone would leave its finished
397
+ per link's batch, not once per session. Each link goes under the transition
398
+ locks of it and the link after it (`SessionTransition`, one try, kind `delete`).
399
+ **A link that can't go now holds the prune there**: one not archived (brought back
400
+ by hand), one whose subtree holds a schedule's anchor, head or target (another
401
+ enabled schedule anchored inside the chain, as `schedule_task` from a link does), a
402
+ live worker or an unarchived descendant in its subtree, or its or the next link's
403
+ transition lock held (a send, a continue, a delete). The prune stops below it: that
404
+ link and every newer one stay, and nothing is deleted around it. The entry records
405
+ where it waits (`prune_waiting` in schedules.json: link, reason, since when; no
406
+ history row), `chi schedule list` adds `pruning held at <id>` to the row and `show` a
407
+ line `pruning held at <id>: <reason> (since …)`, the log says `prune_waiting` once
408
+ per link (not every tick), and every tick tries again, not only after a firing; once
409
+ it gets past, the record goes. A schedule anchored inside the chain holds it until
410
+ that schedule is cancelled. Pruning a link alone would leave its finished
393
411
  delegates archived forever with a dangling `parent_id`: archiving a session archives
394
412
  its descendants, and archived sessions are outside the retention sweep
395
413
  ([sessions.md](sessions.md)), so the delegate pile is the bulk of the archived
396
414
  count. The `anchor` is never pruned: it is the chain's root, the id the user and
397
- the coordinator remember, and `last:<anchor>` must keep resolving.
415
+ the coordinator remember, and `last:<anchor>` must keep resolving. Once the
416
+ anchor's successor is gone, nothing continues the anchor; the chain walks for
417
+ `last:`, `chi send` and notes cross that gap to the entry's `head`
418
+ (`SessionChain::Links#anchor_forward`, [sessions.md](sessions.md), *Session chains*
419
+ → Which link), so the anchor stays the chain's address. A link a previous prune
420
+ already deleted is skipped, so each later firing prunes the next link behind the
421
+ window.
398
422
 
399
423
  What a schedule protects (its anchor, its head and its resolved target, while it is
400
424
  enabled) the retention sweep keeps and a delete refuses: schedules are