Skip to content

docs(plan): Mecatl Studio chat acceptance plan - #1755

Merged
samuv merged 1 commit into
studio/bootstrapfrom
plan/studio-chat
Sep 23, 2026
Merged

samuv merged 1 commit into
studio/bootstrapfrom
plan/studio-chat

Conversation

@samuv

@samuv samuv commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds docs/acceptance/studio-chat.md (Bounded): the first feature contract — the /api/v1/sessions… session inventory and lifecycle routes, the run SSE union (run.started / run.event / run.error, unknown SDK kinds preserved with their raw payload), run controls addressed to the exact durable run, and the chat workspace with its behaviour tests as proofs.

Review focus

  • Scope decisions recorded in Human decisions: whole-feature port including browser-local conveniences; only the UI primitives chat imports; the SSE union replaces the bootstrap's stub stream schema; every mutation behind the CSRF and session gates.
  • The Events/persistence row: the union shape and the studio.chat.* browser storage keys.

Checker: check-acceptance-plan.sh passes; task docs passes. Stacked delivery: nothing in this series merges until the whole stack is reviewed; the plan's approval-by-merge event is deferred to the end, as its Delivery line records.

Amended after review

A review finding against the first draft of this contract: GET /sessions/{id}/activity specified an unconditional replay of a session's whole durable history, with no cursor, window, or cap. An authenticated browser could reattach to a long-lived chat repeatedly and make the backend for frontend and the daemon do unbounded historical reads and stream that history back each time. The published SDK's AttachOptions.from already accepts "now", "start", or a cursor, and every envelope carries an opaque string cursor, so the bound costs no capability.

The contract now says:

  • Every run.event frame carries its durable cursor in the SSE id field; a request resumes exactly after Last-Event-ID (or the resumeFrom query parameter, the header winning).
  • A no-cursor replay forwards at most STUDIO_ACTIVITY_REPLAY_MAX durable replay events (default 2000) and reads no further, then ends with run.truncated {reason: "bound", cursor} pointing the client at the authoritative transcript. Live events stay unbounded so a long turn still streams in full.
  • A durable gap ends the stream with run.truncated {reason: "gap"} rather than streaming across the hole; an unusable cursor is 400 invalid_cursor.
  • A session admits STUDIO_ACTIVITY_MAX_STREAMS concurrent activity streams per replica (default 4), beyond which the route answers 429 too_many_streams with Retry-After.

Three new acceptance criteria (AC2.7, AC2.8, AC2.9) name proofs for bounded replay, exact resume without duplication, the gap case, the invalid cursor, and the stream cap. The two new settings are declared in the CLI/config row. The decision and its rationale are recorded under Human decisions; Deferred decisions and known risks records that the 2000 default is a starting value, not a measured one.

Contract amendments

  • Activity stream. The stream commits its headers at the replay-to-live boundary, so an idle session opens without waiting for a live event. It writes an SSE comment every 15 seconds.
  • Cursors. A start or now cursor, or one over 1,024 characters, is refused with 400 invalid_cursor. A daemon activity_gap error ends the stream as a gap does.
  • Messages and validation. run.error messages are clamped and redacted like problem details. Base64 attachments are validated in one linear pass.
  • Browser behaviour. A browser view follows only the run of the chat it shows. On a bound truncation it reattaches from the cursor (up to 50 times) instead of failing the run.
  • New ACs: AC2.10, AC4.8, and AC4.9, plus extra proofs on AC2.2 and AC2.4. The image keeps the bootstrap's early-access label.
  • Stream shape. The request that delivers the bound-th replay event ends with run.truncated and reads no further envelope (AC2.7). A resumed stream starts with the next durable event, with no leading run.started, while a later run still gets one (AC2.8). A request that saw no run closes at the live boundary unless the session is running, awaiting, or authorizing (AC2.5).
  • Body limits. Request bodies are bounded before parsing: 32 MiB for runs and 8 MiB elsewhere, answering 413 request_too_large (AC2.11).
  • URL-only images. They render as external links, so the content security policy stays 'self' for images (AC4.3).
  • Browser. Side threads follow truncation as the main chat does (AC4.9). A repeated run.started keeps the active message (AC4.10). The chat list and the preview keep separate widths (AC4.11).

Development stage

  • Plan / Interface
  • Implementation
  • Combined
  • Spike / Routine / Cleanup

Contract linkage

  • Work classification: Bounded
  • Classification rationale: adds the chat product surface (/api/v1/sessions… and the run SSE union) inside the boundary ADR 0351 fixed; no new durable architecture decision
  • Decision record: None — the boundary and the published-SDK rule are ADR 0351's; this plan only fills in a feature contract that ADR anticipated
  • Human waiver of spine: Yes — the directing human chose stacked (gh stack) delivery for the whole Studio series: nothing merges until every layer is reviewed, so the plan's approval-by-merge event is deferred to the end (recorded in each plan's Delivery line), and each implementation layer records the exact plan commit it built against.
  • Acceptance plan: docs/acceptance/studio-chat.md
  • Human decisions resolved and recorded: Yes
  • Plan / Interface PR: this PR
  • Approved commit baseline: absent until the stack root merges (stacking exception above)
  • Combined/exemption rationale: N/A

Interface conformance

  • Interface contract under review: Sixteen /api/v1/sessions… routes; the run.started / run.event / run.truncated / run.error SSE union with unknown SDK kinds preserved; 409 stale_run_control on controls for ended runs; studio.chat.* browser storage keys. No gRPC, Go, CLI, or tool-schema change.

Issue relationship

Relates to #1747 (parent #1736; stack #1766). Non-closing: the step completes when both of its layers merge.

Type of change

  • Behavioral/interface plan
  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation/process

Test plan

Baseline checks

  • Acceptance-plan checker
  • Linting (task lint)
  • Offline test suite (task test)
  • Offline demo (go run ./cmd/mecademo)
  • Markdown changed: docs generation/link checks (task docs)

Plan layer: the acceptance-plan checker and task docs pass. The Studio gates (task studio:check, task ac-trace) run on the implementation layer above, since this layer adds no apps/ code.

User-facing change

None — a plan document and, for the bootstrap, ADR 0351.

Special notes for reviewers

Part of a fourteen-layer stack; review bottom-up. Nothing merges until the whole series is reviewed.

@samuv

samuv commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Review finding addressed: unbounded activity replay

Confirmed and fixed. The first draft of this contract had GET /sessions/{id}/activity replay a session's whole durable history on every request, with no cursor, window, or cap, so an authenticated browser could reattach to a long-lived chat repeatedly and make the BFF and the daemon do unbounded historical reads and stream that history back each time.

The published SDK made the bound free: AttachOptions.from accepts "now", "start", or a cursor, and every envelope carries an opaque string cursor.

Contract now (this PR):

  • Every run.event frame carries its durable cursor in the SSE id field. A request resumes exactly after Last-Event-ID, or the resumeFrom query parameter for clients that cannot set that header, the header winning.
  • A no-cursor replay forwards at most STUDIO_ACTIVITY_REPLAY_MAX durable replay events (default 2000) and reads no further, then ends with run.truncated {reason: "bound", cursor} that points the client at GET /sessions/{id}/transcript, which already owns the authoritative history. Live events stay unbounded, so a long turn still streams in full.
  • A durable gap carries no cursor, so the stream ends with run.truncated {reason: "gap"} instead of streaming across the hole.
  • An unusable cursor is 400 invalid_cursor, decided before any SSE header is committed, never a silent full replay.
  • A session admits STUDIO_ACTIVITY_MAX_STREAMS concurrent activity streams per replica (default 4); beyond that the route answers 429 too_many_streams with Retry-After.

New acceptance criteria AC2.7, AC2.8, and AC2.9 name the proofs. The two settings are declared in the CLI/config row, the rationale is recorded under Human decisions, and the risks section notes that 2000 is a starting value rather than a measured one.

Implementation is in the layer above (#1756), with six unit proofs: bounded replay with the truncation cursor, exact resume without duplication, the durable-gap stop, live events staying unbounded, header-versus-query precedence with id stamping, and the invalid-cursor plus stream-cap behaviour.

Acceptance test against a real daemon is in #1767: a tiny replay bound truncates after exactly its quota and names the last cursor, resuming from it returns only events not already delivered, walking the cursors to the run's terminal result never duplicates one and never exceeds the bound in a single request, and an unusable cursor is refused.

One implementation note worth flagging for review: the resume query parameter is resumeFrom, not cursor, because the client generator reads cursor as a pagination marker and emits an infinite-query wrapper that cannot type a Server-Sent Events response.

Agreed on the SDK surface: no primary blocker there. The only risk was this contract baking in the unbounded behaviour, which it no longer does.

Bounded plan for the first Studio feature layer: the session inventory
and lifecycle routes, the run SSE union (run.started / run.event /
run.error, unknown SDK kinds preserved with their raw payload), run
controls addressed to the exact durable run, and the chat workspace
with its ported behaviour tests as proofs. The BFF contract is taken
from the design prototype with one deliberate divergence: activity
replay is BOUNDED and RESUMABLE rather than an unconditional replay from
the start of durable history. It carries a resume cursor (SSE `id` frames
honouring `Last-Event-ID`), bounds a no-cursor replay to
STUDIO_ACTIVITY_REPLAY_MAX events, reports `run.truncated` when the
history exceeds that bound, and caps concurrent streams per session --
answering a blocking review finding that the unconditional replay let one
authenticated browser impose unbounded historical reads on the BFF and the
daemon. The bootstrap's stub stream schema is replaced, and the
experimental image label is removed when this lands.

Tracking: #1736

Signed-off-by: Samuele Verzi <[email protected]>
@samuv
samuv merged commit 431eabd into main Sep 23, 2026
31 checks passed
@samuv
samuv deleted the plan/studio-chat branch September 23, 2026 13:15
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.

1 participant