[GH-ISSUE #1782] [Feature]: Long-Term Memory: give each chat-messages piece the end of the previous piece, its events and open threads #1751

Closed
opened 2026-11-07 16:36:25 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Promansis on GitHub (Oct 27, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1782

Originally assigned to: @Promansis on GitHub.

What problem does this solve?

Importing a chat's messages into Long-Term Memory splits the chat into pieces of about 24k characters and extracts each piece on its own. Each extraction sees only its piece, so two things break where a piece ends:

  1. Causes across the cut are lost. An event in piece 2 that was caused by an event in piece 1 gets no caused_by link. The prompt rules forbid targeting a note outside the current response, so the model can't link back.
  2. Open threads are duplicated. A thread (a plan, promise or unresolved question) that started in piece 1 and continues in piece 2 becomes a second thread note with a new id, instead of an update to the first.

Moving the cut to a scene break does not fix (1): consecutive scenes are usually linked by cause, so a cut exactly between two scenes still drops the link.

Measured on one long roleplay chat (366 messages, about 409k characters) with Sonnet, 2 runs per cut, at two cuts: one mid-scene and one exactly on a scene break.

expected cross-cut cause found duplicate thread at the mid-scene cut
today (plain cut) 0/4 2/2
previous piece's last ~3k characters as context 4/4 2/2
post-hoc boundary pass (one extra model call per cut) 3/4 merged 2/2
context + previous piece's events and open threads 4/4 0/2

With context plus threads, piece 2 updated the existing thread (update_section + add_link) instead of creating a new one. The updated text advanced the thread's state correctly. No units were dropped, and no events from piece 1 were extracted again.

Proposed package or change

Package-only change to Long-Term Memory's chat-messages import. No Engine change.

When a chat_transcript piece is extracted and an earlier piece of the same chat exists, pass the model three context-only blocks:

  • the last ~3,000 characters of the previous piece's transcript, marked "context only, already extracted, do not extract again";
  • the timeline events extracted from the previous piece (id, title), with permission to use them as caused_by / triggered_by targets;
  • the open threads extracted from the previous piece (id, subjectId, title, current text), with the instruction to reuse a listed subjectId to update that thread instead of creating a new one.

The first piece of a chat, and every non-chat source, keep today's prompt exactly.

Pieces of one chat must be prepared in order, so that piece n can read what piece n−1 committed. Different chats still run in parallel.

Minimal fix

Server, under packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/:

  • sources/interop.ts. The chat piece builder (around transcriptPieces and the chat_transcript items, ~670–691) records each piece's predecessor: the previous piece's source note id and the tail of its content. Piece starts recovered from existing source notes (~746) keep working, because the predecessor is simply the previous item of the same chat in previewOrder.
  • extraction/source-processing.ts
    • PrepareOptions gains an optional previousPiece with fields tail, events, threads.
    • processLongTermMemorySourceBatch prepares items that share a chat and have a predecessor after that predecessor commits. Today all items are prepared concurrently, then committed in order. The events and threads are read from the committed vault: notes with extracted_from → the previous piece's source note id, plus the batch overlay. Reading them from the vault also covers a re-import, where piece n−1 was committed in an earlier run.
    • prepareLongTermMemorySource forwards previousPiece to extraction, and passes the listed events and threads as existingNotes so that dedup and link closure know them.
  • extraction/evidence-unit-extraction.ts
    • Render the three blocks next to userInstruction (~1277).
    • Relax two rules only when the blocks are present:
      • serverEnforcedLinkRules() (~75): "Every link target must resolve to sourceNote.id or a target note derived from a unit in the same response" becomes "…or one of the listed previous-piece events and threads".
      • targetNoteRules (~1267): "never target or invent an existing note id" gets the exception "except a listed open thread's subjectId".
  • extraction/evidence-unit-validation.ts: probably no change. closeLtmEvidenceUnitLinks (~415) already accepts existingNotes ids as targets, and hasRelationshipSupport already accepts an existing timeline note as caused_by. The regression proves this.
  • extraction/dedup.ts: check that deduplicateUnits(units, existingNotes, scope) maps a thread unit with a reused subjectId to the existing thread_<subjectId> note as an update, not a create.

Regression: a deterministic test with a fake language model and no network, in the package's tests next to the existing chat-import and extraction tests. A chat is split into two pieces:

  1. Piece 1's prompt has no previous-piece blocks.
  2. Piece 2's prompt carries piece 1's tail, its event ids and its open thread.
  3. A piece 2 event with caused_by → a piece 1 event id survives validation and link closure, and is committed.
  4. A piece 2 thread unit that reuses piece 1's thread subjectId produces an update to the existing thread note. The vault ends with one thread, not two.
  5. A piece 2 link to an id that is not listed is still rejected.

Non-goals

  • The relationship update_section:state "rewrite" disposition, which forces one-at-a-time apply on later pieces (drafts/reconciliation.ts ~958). That is a separate issue.
  • Scene-break cut points, and a "Check boundaries" pass for chats already imported. These are possible follow-ups.

Why should this be optional?

Long-Term Memory is already a catalog package, and this changes only its own extraction prompt for chat-messages imports. No new setting is needed: the extra blocks add about 3–5k characters to each piece after the first.

Alternatives considered

  • Cut at scene breaks. A prototype put 4 of 6 cuts on scene starts, against 0 of 5 today. But a cut on a true scene break still lost the cross-cut cause (0/4), because consecutive scenes are linked by cause.
  • Previous piece's tail only. This recovers causes (4/4) but still duplicates threads (2/2), because the model can't refer to the thread it can't see.
  • Post-hoc boundary pass. One extra model call per cut proposes links and merges between the two sides. It found 3/4 causes and merged the duplicate threads, at about $0.02–0.03 per boundary. Useful later for chats already imported, but it is a second call and a second review step for every cut.

Additional context

  • Validation already allows existing targets. Only the prompt rules forbid them, so the change is mostly prompt and ordering.
  • The cost is per-chat serial preparation for multi-piece chats. A single chat's import gets slower; imports of many chats keep their parallelism.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 27, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1782 Originally assigned to: @Promansis on GitHub. ## What problem does this solve? Importing a chat's messages into Long-Term Memory splits the chat into pieces of about 24k characters and extracts each piece on its own. Each extraction sees only its piece, so two things break where a piece ends: 1. **Causes across the cut are lost.** An event in piece 2 that was caused by an event in piece 1 gets no `caused_by` link. The prompt rules forbid targeting a note outside the current response, so the model can't link back. 2. **Open threads are duplicated.** A thread (a plan, promise or unresolved question) that started in piece 1 and continues in piece 2 becomes a second thread note with a new id, instead of an update to the first. Moving the cut to a scene break does not fix (1): consecutive scenes are usually linked by cause, so a cut exactly between two scenes still drops the link. Measured on one long roleplay chat (366 messages, about 409k characters) with Sonnet, 2 runs per cut, at two cuts: one mid-scene and one exactly on a scene break. | | expected cross-cut cause found | duplicate thread at the mid-scene cut | |---|---|---| | today (plain cut) | 0/4 | 2/2 | | previous piece's last ~3k characters as context | 4/4 | 2/2 | | post-hoc boundary pass (one extra model call per cut) | 3/4 | merged 2/2 | | **context + previous piece's events and open threads** | **4/4** | **0/2** | With context plus threads, piece 2 updated the existing thread (`update_section` + `add_link`) instead of creating a new one. The updated text advanced the thread's state correctly. No units were dropped, and no events from piece 1 were extracted again. ## Proposed package or change Package-only change to Long-Term Memory's chat-messages import. No Engine change. When a `chat_transcript` piece is extracted and an earlier piece of the same chat exists, pass the model three context-only blocks: - the last ~3,000 characters of the previous piece's transcript, marked "context only, already extracted, do not extract again"; - the timeline events extracted from the previous piece (`id`, `title`), with permission to use them as `caused_by` / `triggered_by` targets; - the open threads extracted from the previous piece (`id`, `subjectId`, `title`, current text), with the instruction to reuse a listed `subjectId` to update that thread instead of creating a new one. The first piece of a chat, and every non-chat source, keep today's prompt exactly. Pieces of one chat must be prepared in order, so that piece n can read what piece n−1 committed. Different chats still run in parallel. ### Minimal fix Server, under `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/`: - `sources/interop.ts`. The chat piece builder (around `transcriptPieces` and the `chat_transcript` items, ~670–691) records each piece's predecessor: the previous piece's source note id and the tail of its `content`. Piece starts recovered from existing source notes (~746) keep working, because the predecessor is simply the previous item of the same chat in `previewOrder`. - `extraction/source-processing.ts` - `PrepareOptions` gains an optional `previousPiece` with fields `tail`, `events`, `threads`. - `processLongTermMemorySourceBatch` prepares items that share a chat and have a predecessor after that predecessor commits. Today all items are prepared concurrently, then committed in order. The events and threads are read from the committed vault: notes with `extracted_from` → the previous piece's source note id, plus the batch overlay. Reading them from the vault also covers a re-import, where piece n−1 was committed in an earlier run. - `prepareLongTermMemorySource` forwards `previousPiece` to extraction, and passes the listed events and threads as `existingNotes` so that dedup and link closure know them. - `extraction/evidence-unit-extraction.ts` - Render the three blocks next to `userInstruction` (~1277). - Relax two rules only when the blocks are present: - `serverEnforcedLinkRules()` (~75): "Every link target must resolve to sourceNote.id or a target note derived from a unit in the same response" becomes "…or one of the listed previous-piece events and threads". - `targetNoteRules` (~1267): "never target or invent an existing note id" gets the exception "except a listed open thread's subjectId". - `extraction/evidence-unit-validation.ts`: probably no change. `closeLtmEvidenceUnitLinks` (~415) already accepts `existingNotes` ids as targets, and `hasRelationshipSupport` already accepts an existing timeline note as `caused_by`. The regression proves this. - `extraction/dedup.ts`: check that `deduplicateUnits(units, existingNotes, scope)` maps a thread unit with a reused subjectId to the existing `thread_<subjectId>` note as an update, not a create. Regression: a deterministic test with a fake language model and no network, in the package's tests next to the existing chat-import and extraction tests. A chat is split into two pieces: 1. Piece 1's prompt has no previous-piece blocks. 2. Piece 2's prompt carries piece 1's tail, its event ids and its open thread. 3. A piece 2 event with `caused_by` → a piece 1 event id survives validation and link closure, and is committed. 4. A piece 2 thread unit that reuses piece 1's thread subjectId produces an update to the existing thread note. The vault ends with one thread, not two. 5. A piece 2 link to an id that is not listed is still rejected. ### Non-goals - The relationship `update_section:state` "rewrite" disposition, which forces one-at-a-time apply on later pieces (`drafts/reconciliation.ts` ~958). That is a separate issue. - Scene-break cut points, and a "Check boundaries" pass for chats already imported. These are possible follow-ups. ## Why should this be optional? Long-Term Memory is already a catalog package, and this changes only its own extraction prompt for chat-messages imports. No new setting is needed: the extra blocks add about 3–5k characters to each piece after the first. ## Alternatives considered - **Cut at scene breaks.** A prototype put 4 of 6 cuts on scene starts, against 0 of 5 today. But a cut on a true scene break still lost the cross-cut cause (0/4), because consecutive scenes are linked by cause. - **Previous piece's tail only.** This recovers causes (4/4) but still duplicates threads (2/2), because the model can't refer to the thread it can't see. - **Post-hoc boundary pass.** One extra model call per cut proposes links and merges between the two sides. It found 3/4 causes and merged the duplicate threads, at about $0.02–0.03 per boundary. Useful later for chats already imported, but it is a second call and a second review step for every cut. ## Additional context - Validation already allows existing targets. Only the prompt rules forbid them, so the change is mostly prompt and ordering. - The cost is per-chat serial preparation for multi-piece chats. A single chat's import gets slower; imports of many chats keep their parallelism. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-11-07 16:36:25 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Agents#1751
No description provided.