Skip to content

feat(serve): resume a session by id with POST /sessions {"resume"} - #545

Merged
ajianaz merged 1 commit into
developfrom
feat/serve-resume-by-id
Nov 10, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/serve-resume-by-id

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What

Second of three parts of #524 (UI epic #523): POST /sessions {"resume": "<id>"} on tole serve reopens an existing session, so a UI client can pick a session up again after a serve restart (the live map is in memory; the JSONL is what survives). Part 1 (GET /sessions/{id}/entries) is merged (#544). Part 3 (SSE events) follows. Refs #524, does not close it.

Why

POST /sessions only creates, and GET /sessions lists only live sessions, so a client cannot reopen history it can now read. The request shape was agreed with the owner on 2026-11-10.

Changes

  • serve.rs: resume_session + open_serve_session. Create and resume now share the approver, open_session and admission code (extracted from the create path; behavior of creating a session is unchanged, covered by the existing tests).
    • The id is validated (is_valid_session_id) and only joined into the session directory (--sessions-dir, else <cwd>/.tole/sessions); the client never supplies a path. cwd goes through the same jail-of-jails check as a new session (403 outside it), and a session is found only under the workspace it was created in (404 otherwise).
    • Already live: 200 {"resumed": false, "alreadyLive": true, "busy": <bool>}. It is checked first and nothing is reopened or replaced: the writer lock is held by this very process (a second open would be refused as locked), and a running turn must not be disturbed. A client that reconnects mid-turn gets its id and the busy flag.
    • 404 no such file; 400 bad id or a non-string resume; 409 another process (a TUI, a second serve) holds the session's writer lock; 503 at the live-session cap with every session busy (unchanged rule).
    • A resumed session uses the same QueueApprover: non-preauthorized Writes queue for a remote decision exactly as for a new session. Resuming grants no authority; the token is the same.
  • Docs in the same PR (AGENTS.md §7): README status line, docs/mobile-control.md (route table and a Resume by id section, and the "not readable until resumed" note from part 1), docs/epics.md, docs/daemon-design.md, CHANGELOG, serve.rs header.

Testing

  • cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo check -p tole-cli --no-default-features, cargo test --workspace (1163 passed)
  • Local cora review --staged: not run (cora is not installed in this environment); the CI Cora Review job is the verdict.
  • 3 new e2e tests against the real tole serve binary (same file and harness as the transcript tests):
    • a session is gone from a restarted serve (GET …/entries is a 404), resume brings it back with its full history, a new prompt extends the same log;
    • resuming a live session is a no-op even mid-turn (alreadyLive, busy: true), and the 2.5 s turn still completes normally;
    • refusals: ../etc/passwd, empty and non-string ids (400), an unknown id (404), cwd: "/" (403), a jailed sibling directory (404 for a session that is not live), and a second serve process over the same workspace while the first holds the lock (409, nothing opened), then 200 once the holder is gone.
  • Mutation: skipping the live-session check makes the live-session test fail.

Limits

  • Resume does not replay an unfinished turn by itself: a session that was mid-turn when the server died resumes into whatever state its log holds, and the client continues with a prompt (the existing resume rules apply).
  • A live session ignores a mismatching cwd in the request (it is already open under its own workspace).

Related Issues

Refs #524, part of #523.


Generated by Claude Code

Second of three parts of #524. Reopens an existing session (e.g. after a serve restart): id validated and joined into the session dir only, same jail on cwd, an already-live session is returned as is (never reopened, a running turn is not disturbed), 404 unknown, 409 when another process holds the writer lock, 503 at the cap. Create and resume share open_serve_session.
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 31a3f37 into develop Nov 10, 2026
15 of 16 checks passed
ajianaz added a commit that referenced this pull request Nov 10, 2026
## What

Third and last part of #524 (UI epic #523): `GET
/sessions/{id}/events?after=<seq>`, a Server-Sent Events stream so a
graphical client can show a session live. Parts 1 and 2 (transcript read
#544, resume-by-id #545) are merged. This completes #524. Closes #524.

## Why

`POST /sessions/{id}/prompt` blocks until the turn ends, so a UI could
not show streamed text, tool cards or approval requests as they happen.
Design choices were agreed with the owner on 2026-11-10: SSE, durable
frames from the log plus best-effort ephemeral frames, and a separate
cap of 8 streams.

## Changes

- `crates/tole-cli/src/events.rs` (new, lib), pure and unit-tested:
- SSE framing; **durable** `entry` frames use the entry's `seq` as the
SSE `id` (so `Last-Event-ID` and `?after=` replay losslessly),
**ephemeral** frames carry no id so a reconnect can never skip a real
entry.
- `Bus` / `BusRegistry` / `BusObserver`: fan-out of a turn's
`TurnObserver` callbacks (`text_delta`, `reasoning`, `tool_started`,
`tool_finished`) to open streams through a **bounded, non-blocking**
queue per stream; a full queue drops the frame, counts it and the stream
says `resync`. A slow client can never slow a turn.
- `EntryTail`: follows the session file by `seq` (one `stat` while
unchanged; a read-only replay when it changed, a torn tail is skipped,
never truncated), so it works while a turn holds the live storage lock.
Writes that land between the stat and the replay are picked up next
poll, never missed (seq filters duplicates).
- `ApprovalWatch`: `approval_requested` / `approval_resolved` frames
from the existing approvals queue (a late joiner sees what is pending;
old decisions are not replayed).
- `StreamLimiter`: at most 8 open streams (`429` beyond that), freed on
drop.
- `Heartbeat`: a pure clock that is only reset by a real write (injected
time, unit-tested); `TOLE_SERVE_HEARTBEAT_SECS` (1 to 3600, default 15)
overrides the interval, e.g. behind a proxy with a short idle timeout.
- `serve.rs`: `Last-Event-ID` is parsed; the stream is served before
normal routing and owns its connection (`ready` marks the end of the
replay, then durable entries, approvals and ephemeral frames, a
heartbeat comment after 15 s without a write, and `closed` when the
session leaves the live map). Streams count within the 32-connection cap
but cannot starve `/approvals` and `/prompt`. Approval frames go out
before the durable entries of the same pass, so a decision is announced
ahead of its effects.
- The prompt route attaches an observer **only while a stream is open on
that session**. Attaching an observer turns on provider streaming, so
with nobody watching a turn runs exactly as before streams existed (zero
change for existing REST users). Consequence, documented: a stream
opened mid-turn gets durable entries but no text deltas for that turn.
- Tests: the serve process, provider mock and a small SSE client moved
into a shared `tests/serve_harness` (the entries/resume tests from
#544/#545 use it too).
- Docs in the same PR (AGENTS.md §7): README status line,
`docs/mobile-control.md` (route table and an Event stream section with
the frame table, replay semantics, limits and the browser `EventSource`
caveat), `docs/epics.md`, `docs/daemon-design.md`,
`docs/threat-model.md` (new Event streams section), CHANGELOG,
`serve.rs` header.

## Not included (stated, not hidden)

- An observer for the turn that resumes after an approval `allow`: that
path (`resume_turn_with_cancel`) has no observer variant in `tole-core`,
so adding one is a core change beyond this issue. Its entries (the final
answer) still arrive as durable frames; only live text/tool frames are
missing for it.
- A stream token for browser `EventSource` (cannot send
`Authorization`): left to the web client (#529).
- A log index: each change replays the session file, so a very large
session costs more per change.

## Testing

- [x] `cargo fmt --all -- --check`, `cargo clippy --workspace
--all-targets -- -D warnings`, `cargo check -p tole-cli
--no-default-features`, `cargo test --workspace` (1178 passed)
- [ ] Local `cora review --staged`: not run (cora is not installed in
this environment); the CI Cora Review job is the verdict.
- [x] 10 unit tests (`events`): frame format and escaping, ephemeral
shapes, bus fan-out/drop counting/unsubscribe, observer only while
watched, limiter, approval diff (late joiner, resolve, expiry,
decided-between-polls), tail (each entry once, torn tail, unchanged = no
replay, start after a seq), the heartbeat clock (only a real write
resets it) and its interval parsing.
- [x] 5 e2e tests against the real `tole serve` binary: replay then
follow a live turn (entries equal the stored log, ids are seqs, ordered,
no duplicates, a `text_delta` arrives, the prompt answer is unchanged);
reconnect with `Last-Event-ID` and with `?after=` gets exactly the
entries it missed, no stray frames, `after` beats a stale header;
401/404/400 and the cap (8 open, the 9th is 429, `/sessions` and
`/approvals` still answer); approval flow (`approval_requested`, allow,
`approval_resolved` before the resumed turn's answer, the work
executed); an idle stream heartbeats (interval set to 1 s).
- Mutations, each caught: no observer attached (live-turn test), `seq
>=` in the tail (duplicates: e2e + unit), cap off by one (e2e + unit),
an empty approvals pass resetting the heartbeat clock (the bug Cora
found: heartbeat e2e times out).

## Related Issues

Closes #524, part of #523. Follow-ups in the epic: #525, #526.

---
_Generated by [Claude
Code](https://claude.ai/code/session_01GwJr9GzLbDFeXo3HZHZpyP)_

---------

Co-authored-by: Claude <[email protected]>
@ajianaz
ajianaz deleted the feat/serve-resume-by-id branch November 11, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants