Skip to content

feat(loop): persist loop settings on un-loop; symmetric re-loop restore (mitto-0do)#68

Merged
inercia merged 2 commits into
mainfrom
pr/loop-persistence-symmetric
Jul 5, 2026
Merged

feat(loop): persist loop settings on un-loop; symmetric re-loop restore (mitto-0do)#68
inercia merged 2 commits into
mainfrom
pr/loop-persistence-symmetric

Conversation

@inercia

@inercia inercia commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Summary

Extends the archive/unarchive loop-persistence feature (mitto-vmp) to the un-loop button, and makes loop ⇄ un-loop a symmetric, non-destructive toggle across every interface (REST and MCP).

Previously, pressing Unloop (DELETE /api/sessions/{id}/loop) hard-deleted loop.json, discarding the configuration. Now it detaches: loop.json is moved to loop.saved.json so the prompt / frequency / trigger / enabled state survive, and the conversation reverts to a regular one (loop_configured=false) exactly as before. Pressing Make loop restores the saved settings — preserving the enabled state they had at un-loop time (an active loop resumes; a paused draft comes back paused). Iteration/duration counters and the stopped reason are reset so the restored loop starts its budget fresh.

Closes mitto-0do.

Changes

Session layerinternal/session/loop.go

  • New loop.saved.json slot + LoopStore.Detach / GetSaved / ClearSaved.

RESTinternal/web/handlers

  • handleDeleteLoop now detaches instead of deleting (same 204 + loop_updated(nil) broadcast).
  • New POST /api/sessions/{id}/loop/restore (handleRestoreLoop): restores the saved config, resets counters, clears the saved slot, re-broadcasts, bootstraps onCompletion; 404 when nothing is saved.
  • handleSetLoop clears the saved slot after Set so a freshly-defined loop supersedes stale saved settings.

MCPinternal/mcpserver/server.go

  • On create-loop (isNew) via mitto_conversation_update, clear the saved slot after Set — parity with the REST make-loop path.

Frontendweb/static

  • handleMakeLoop tries POST /loop/restore first, falling back to a blank draft PUT on 404; un-loop toast notes settings are saved for later restore.
  • Added endpoints.sessions.loopRestore.

Design notes

  • Symmetric enabled state: re-loop restores the exact enabled state (active vs. paused) the loop had when detached — like archive ⇄ unarchive.
  • loop_enabled:false via MCP stays "pause" (keeps loop.json via MarkStopped); it is intentionally not a full un-loop. The consistency fix therefore lives on the create-loop side (clear stale saved slot), not on disable.
  • Clear is explicit, not inside Set(): the restore handler calls Set(saved) then ClearSaved(); keeping the lifecycle explicit matches the existing Detach/GetSaved/ClearSaved style.

Testing

  • Session: Detach/GetSaved/ClearSaved round-trips, not-found paths, overwrite-latest.
  • REST: full un-loop → restore round-trip (prompt + enabled state preserved, counters reset, saved slot cleared), restore-404, set-clears-saved-slot.
  • MCP: create-loop clears the saved slot.

Gate results

gofmt clean · go build ./... ok · go vet clean · go test ./internal/session ./internal/web/handlers ./internal/mcpserver pass · node --check on app.js + endpoints.js ok.

…re (mitto-0do)

Extend the archive/unarchive loop-persistence feature (mitto-vmp) to the
un-loop button and make loop <-> un-loop a symmetric, non-destructive toggle
across every interface (REST and MCP).

Previously pressing "Unloop" (DELETE /api/sessions/{id}/loop) hard-deleted
loop.json, discarding the configuration. Now it detaches: loop.json is moved
to loop.saved.json so the prompt/frequency/trigger and enabled state survive,
and the conversation reverts to a regular one (loop_configured=false) exactly
as before. Pressing "Make loop" restores the saved settings, preserving the
enabled state they had at un-loop time (an active loop resumes; a paused draft
comes back paused). Iteration/duration counters and the stopped reason are
reset so the restored loop starts its budget fresh.

Session layer (internal/session/loop.go):
- Add loop.saved.json slot and LoopStore.Detach / GetSaved / ClearSaved.

REST (internal/web/handlers):
- handleDeleteLoop now Detaches instead of Deletes (same 204 + nil broadcast).
- Add POST /api/sessions/{id}/loop/restore (handleRestoreLoop): restores the
  saved config, resets counters, clears the saved slot, re-broadcasts, and
  bootstraps onCompletion; 404 when nothing is saved.
- handleSetLoop clears the saved slot after Set so a freshly-defined loop
  supersedes stale saved settings (keeps the toggle symmetric).

MCP (internal/mcpserver/server.go):
- On create-loop (isNew) via mitto_conversation_update, clear the saved slot
  after Set - parity with the REST make-loop path.

Frontend (web/static):
- handleMakeLoop tries POST /loop/restore first, falling back to a blank draft
  PUT on 404; un-loop toast notes settings are saved for later restore.
- Add endpoints.sessions.loopRestore.

Tests:
- Session: Detach/GetSaved/ClearSaved round-trips, not-found paths, overwrite.
- REST: full un-loop -> restore round-trip (prompt + enabled state preserved,
  counters reset, saved slot cleared), restore-404, set-clears-saved-slot.
- MCP: create-loop clears the saved slot.
Copilot AI review requested due to automatic review settings July 5, 2026 12:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes loop ⇄ un-loop a symmetric, non-destructive toggle across REST/MCP/UI by preserving loop settings on un-loop (detach to loop.saved.json) and restoring them on re-loop (new restore endpoint + UI behavior).

Changes:

  • Session storage: add a saved loop slot (loop.saved.json) with Detach, GetSaved, and ClearSaved.
  • REST + UI: DELETE /loop now detaches (preserves settings) and the UI attempts POST /loop/restore before creating a blank draft loop.
  • REST + MCP parity: clearing the saved slot after a fresh loop Set to prevent stale restores; add tests covering detach/restore flows.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
web/static/utils/endpoints.js Adds a frontend endpoint helper for POST /api/sessions/{id}/loop/restore.
web/static/app.js UI “Make loop” first attempts restore; un-loop toast now communicates settings are preserved.
internal/web/handlers/session_loop.go Routes the new restore sub-path to a dedicated handler.
internal/web/handlers/session_loop_write.go Clears any saved slot after a fresh PUT loop set to avoid stale restore.
internal/web/handlers/session_loop_test.go Adds REST tests for un-loop → restore round-trip, restore-404, and set-clears-saved.
internal/web/handlers/session_loop_run.go Implements detach-on-delete and the new restore endpoint.
internal/session/loop.go Adds loop.saved.json support and LoopStore APIs for detach/saved management.
internal/session/loop_test.go Adds unit tests for Detach, GetSaved, ClearSaved, and overwrite semantics.
internal/mcpserver/server.go Clears the saved slot after MCP create-loop (isNew) for parity with REST.
internal/mcpserver/server_test.go Adds MCP test verifying create-loop clears any previously-detached saved slot.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/web/handlers/session_loop_run.go
…iew)

Copilot review on PR #68 correctly flagged that handleRestoreLoop called
ps.Set(saved) without first ensuring loop.json was absent. If both loop.json
and loop.saved.json existed (e.g. a previous ClearSaved silently failed),
LoopStore.Set() would follow its update path and silently preserve
CreatedAt/IterationCount/FirstRunAt/LastSentAt from the active config —
undoing the counter reset in the restore handler and potentially clobbering
a newly-defined loop with stale saved settings.

Add a defense-in-depth guard: probe ps.Get() before touching the saved slot
and reject with 409 Conflict when an active loop is already configured. Only
proceed to GetSaved/Set/ClearSaved when Get returns ErrLoopNotFound. Keep
the existing 404 behaviour for the empty-saved-slot case.

Test: TestHandleSessionLoop_RestoreRejectedWhenLoopActive seeds a loop,
detaches it (populating loop.saved.json), installs a fresh active loop via
the raw store (bypassing the handler's ClearSaved), bumps IterationCount
via RecordSent, then POSTs /loop/restore and asserts 409 plus that
Prompt/IterationCount/FirstRunAt/LastSentAt on the active loop are all
unchanged and the saved slot is preserved for later conflict resolution.
@inercia
inercia merged commit 630d90f into main Jul 5, 2026
4 checks passed
@inercia
inercia deleted the pr/loop-persistence-symmetric branch July 5, 2026 15:23
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