-
Notifications
You must be signed in to change notification settings - Fork 782
fix(memos-local-plugin): prevent orphan episode scan from closing active sessions; add episode delete API #1546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3bf3ead
328d1a5
27d64eb
f2571e7
68bfe16
bfee04d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,9 +332,19 @@ export function createMemoryCore( | |
| // to `closed` + sets `closeReason: "abandoned"` without touching | ||
| // trace_ids_json. | ||
| try { | ||
| const orphans = handle.repos.episodes.list({ status: "open", limit: 500 }); | ||
| const openEpisodes = handle.repos.episodes.list({ status: "open", limit: 500 }); | ||
| // Only treat an open episode as an orphan if its session has been | ||
| // explicitly closed (meta.closedAt is set) or no longer exists. | ||
| // Otherwise the session might reconnect — leave it alone. | ||
| const orphans = openEpisodes.filter((ep) => { | ||
| const session = handle.repos.sessions.getById(ep.sessionId); | ||
| if (!session) return true; // session row gone — orphan | ||
| if (session.meta?.closedAt != null) return true; // explicitly closed — orphan | ||
| return false; // session exists and not closed — skip, might reconnect | ||
| }); | ||
| if (orphans.length > 0) { | ||
| log.info("init.orphan_episodes.close", { count: orphans.length }); | ||
| const skipped = openEpisodes.length - orphans.length; | ||
| log.info("init.orphan_episodes.close", { count: orphans.length, skipped }); | ||
| const endedAt = Date.now(); | ||
| for (const ep of orphans) { | ||
| try { | ||
|
|
@@ -632,6 +642,34 @@ export function createMemoryCore( | |
| handle.sessionManager.finalizeEpisode(episodeId); | ||
| } | ||
|
|
||
| function assertEpisodeDeletable(episodeId: EpisodeId): void { | ||
| const snap = handle.sessionManager.getEpisode(episodeId); | ||
| if (snap?.status === "open") { | ||
| throw new MemosError( | ||
| "conflict", | ||
| `cannot delete open episode: ${episodeId}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async function deleteEpisode(episodeId: EpisodeId): Promise<{ deleted: boolean }> { | ||
| ensureLive(); | ||
| assertEpisodeDeletable(episodeId); | ||
| return { deleted: handle.repos.episodes.deleteById(episodeId) }; | ||
| } | ||
|
Comment on lines
+655
to
+659
|
||
|
|
||
| async function deleteEpisodes(ids: readonly EpisodeId[]): Promise<{ deleted: number }> { | ||
| ensureLive(); | ||
|
Starfie1d1272 marked this conversation as resolved.
|
||
| for (const id of ids) { | ||
| assertEpisodeDeletable(id); | ||
| } | ||
| let deleted = 0; | ||
| for (const id of ids) { | ||
| if (handle.repos.episodes.deleteById(id)) deleted++; | ||
| } | ||
| return { deleted }; | ||
| } | ||
|
|
||
| // ─── Pipeline (per turn) ── | ||
| async function onTurnStart( | ||
| turn: Parameters<MemoryCore["onTurnStart"]>[0], | ||
|
|
@@ -1947,6 +1985,8 @@ export function createMemoryCore( | |
| closeSession, | ||
| openEpisode, | ||
| closeEpisode, | ||
| deleteEpisode, | ||
| deleteEpisodes, | ||
| onTurnStart, | ||
| onTurnEnd, | ||
| submitFeedback, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new orphan-episode filter changes boot-time behavior in a subtle but reliability-critical way (preserving open episodes when the owning session wasn’t explicitly closed). There are existing
MemoryCoretests (e.g.tests/unit/pipeline/memory-core.test.ts); please add coverage for this init-path so regressions don’t reintroduce accidental orphan-closing on restart.