feat(loop): persist loop settings on un-loop; symmetric re-loop restore (mitto-0do)#68
Merged
Merged
Conversation
…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.
There was a problem hiding this comment.
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) withDetach,GetSaved, andClearSaved. - REST + UI:
DELETE /loopnow detaches (preserves settings) and the UI attemptsPOST /loop/restorebefore creating a blank draft loop. - REST + MCP parity: clearing the saved slot after a fresh loop
Setto 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.
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-deletedloop.json, discarding the configuration. Now it detaches:loop.jsonis moved toloop.saved.jsonso 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 layer —
internal/session/loop.goloop.saved.jsonslot +LoopStore.Detach/GetSaved/ClearSaved.REST —
internal/web/handlershandleDeleteLoopnow detaches instead of deleting (same204+loop_updated(nil)broadcast).POST /api/sessions/{id}/loop/restore(handleRestoreLoop): restores the saved config, resets counters, clears the saved slot, re-broadcasts, bootstraps onCompletion;404when nothing is saved.handleSetLoopclears the saved slot afterSetso a freshly-defined loop supersedes stale saved settings.MCP —
internal/mcpserver/server.goisNew) viamitto_conversation_update, clear the saved slot afterSet— parity with the REST make-loop path.Frontend —
web/statichandleMakeLooptriesPOST /loop/restorefirst, falling back to a blank draftPUTon404; un-loop toast notes settings are saved for later restore.endpoints.sessions.loopRestore.Design notes
loop_enabled:falsevia MCP stays "pause" (keepsloop.jsonviaMarkStopped); 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.Set(): the restore handler callsSet(saved)thenClearSaved(); keeping the lifecycle explicit matches the existingDetach/GetSaved/ClearSavedstyle.Testing
Detach/GetSaved/ClearSavedround-trips, not-found paths, overwrite-latest.Gate results
gofmtclean ·go build ./...ok ·go vetclean ·go test ./internal/session ./internal/web/handlers ./internal/mcpserverpass ·node --checkonapp.js+endpoints.jsok.