[GH-ISSUE #1448] [Issue]: Long-Term Memory: provider subject keys misattribute facts, late unit drops leave dangling links, blank templates use the Roleplay prompt #734

Closed
opened 2026-10-20 17:30:11 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @Promansis on GitHub.

Summary

Three extraction defects in evidence-unit-extraction.ts, filed together because they change the same file (tracker items EX-I2, EX-I1 and EX-I9):

  1. A provider-supplied subjectKeys value silently binds a fact to the wrong character. The prompt hands the model every catalog key, tells it never to use them, and the server honours the field anyway. The resulting misattribution is only a warning, so it can auto-apply.
  2. A unit that links to a unit dropped after link closure compiles with a dangling link. Low-risk auto-apply then fails the whole source at commit, and in manual review the mutation is blocked with a raw error the reviewer cannot repair.
  3. A whitespace-only prompt template makes Conversation and Game extraction run with the Roleplay prompt.

Affected package

long-term-memory 1.4.48 (upstream/staging 1f7954305).

  • S = packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory
  • SH = packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory

Expected behavior

  1. When the server resolves subjects from subjectNames, a subjectKeys value in the provider response is ignored, and the prompt catalog carries only names and aliases.
  2. At the end of compilation, every create_note link target and add_link target is an existing note, the source note, or a note the same draft creates. A unit whose only grounding link pointed at a dropped unit is dropped too and counted as a validation rejection; a non-structural dangling link is stripped with a diagnostic.
  3. A template that is empty after trimming falls back to that mode's default prompt.

Actual behavior

  1. Unit subjectNames: ["Alice"], subjectKeys: [<Bob's key>], text "Alice prefers tea over coffee" resolves to Bob with only a subject_identity_normalized warning. (Extraction stages audit, probe F.)
  2. Four units where a thread with claimKind: "change" and no event link is dropped by closeSourceEventGraph: the world_fact that linked involves -> thread_coffee_story compiles as create_note with that link intact, requiresReview: false. Preflight then throws Long-term memory draft link target not found: thread_coffee_story and the source is reported failed at finalize with the fingerprint already persisted, so a retry reproduces it. (Probe B.)
  3. A template whose prompt is only whitespace is active for Conversation mode: extraction runs with DEFAULT_LTM_EXTRACTION_PROMPT (the Roleplay text).

All locations below were checked at 1f7954305.

Steps to reproduce

  1. Wrong key. Call runLongTermMemoryEvidenceUnitExtraction with a trusted catalog of Alice Vance and Bob Reyes and a provider response whose one character_fact unit names Alice and carries Bob's key. The mutation targets char_bob_reyes.
  2. Dangling link. Compile the probe-B response (a thread unit with claimKind: "change" and no event link, plus a world_fact linking involves to it) with applyLowRisk. The draft commit throws at preflight.
  3. Blank template. In Settings → Extraction prompts, make a template whose text is spaces only, set it active for Conversation, and extract a Conversation source with debug logging on: the request's system prompt is the Roleplay default.

Minimal fix

One PR, patch bump.

1. Ignore provider keys (S/evidence-unit-extraction.ts, S/subject-identity.ts)

  • normalizedEvidenceUnitRecord (:609-616) only drops an empty subjectKeys array. When resolveSubjectNames is on, drop the field altogether; pass the flag in from the parser. The internal resolver still sets subjectKeys itself for established and borrowed keys (S/subject-identity.ts:1279-1291), so that path is unchanged.
  • evidenceUnitResponseFormat (:422, field at :506-510): omit subjectKeys from the provider JSON schema when resolveSubjectNames is on.
  • trustedLtmSubjectPromptCatalog (S/subject-identity.ts:703-714): stop serializing key and ref; the model is asked to copy names and aliases only. Check that nothing but the prompt reads this output.
  • The rule at :1263 can keep its target-id half; the "database subject keys" half becomes moot.

2. Re-close links after late drops (S/evidence-unit-extraction.ts, compile section)

  • Link closure runs once, inside validateLtmEvidenceUnits (S/evidence-unit-validation.ts:392-442). Three later passes drop units without re-closing: deduplicateUnits (:1533), dropLocalCharactersOutsideRoleplay (:1534-1538, :1672) and closeSourceEventGraph (:1539, :1702). The compiler copies unit links verbatim.
  • Fix: after closeSourceEventGraph (and again for duplicateAliasClosure.units, :1560), run the validator's link-closure step over the surviving units, with the valid-target set rebuilt from the survivors plus existingNotes and the source note, to a fixpoint with closeSourceEventGraph. Export that step from the validator rather than copying it. Strip a non-structural dangling link and emit a diagnostic; drop the unit when the dangling link was the one that satisfied relationship support or event grounding, with validator code unknown_link_target. Add the drops to allDroppedCandidates and droppedCandidateCount (:1567-1581) so the accounting still balances.
  • Live triggers at HEAD, for the fixture variants: a static unit linking to a change unit without an event link (probe B); a timeline event with affects_character to a character unit whose claimKind is change and that has no link back; any link to a local-character unit dropped outside Roleplay.

3. Blank template falls back by mode (S/extraction-config.ts, S/evidence-unit-extraction.ts)

  • getLtmExtractionConfig (S/extraction-config.ts:50-58) returns template.prompt untrimmed; treat a template whose trimmed prompt is empty as absent, so the existing fallback at :59-62 picks the mode default.
  • evidenceUnitMessages (:1160-1161) falls back to DEFAULT_LTM_EXTRACTION_PROMPT; use DEFAULT_LTM_EXTRACTION_PROMPTS_BY_MODE[options.mode ?? options.modes[0] ?? "roleplay"], the same expression :1530 uses for allowedBuckets.
  • Do not tighten SH/schema.ts:177 (prompt: z.string().min(1)) with .trim(): stored settings that already hold a blank template would then fail to parse on load.

CHANGELOG under ## Unreleased: one bullet each for 1, 2 and 3, written for users (which character a fact is saved to; imports no longer fail or block review on a dropped link; blank templates).

Not included

  • Resolving link targets outside the retrieval window (audit F7, part 2) and the prompt-envelope work (F5, F6, tracker EX-I5). EX-I5 touches the same file and should follow this PR.
  • A dedicated drop reason for unknown_link_target; the message fix is tracker EX-I6, in a separate issue.

Regression proof

  • tests/long-term-memory-local-characters.regression.ts (already exercises subjectKeys, :207-333, :716-722): the probe-F pair resolves to Alice with and without the wrong key; the serialized trustedSubjects carries no key or ref; the established-key and borrowed-key cases still pass. Cases that model provider output and expect a provider key to win change to expect name resolution.
  • tests/long-term-memory-extraction-graph.regression.ts: the probe-B fixture plus the affects_character and local-character variants. Assert that no compiled create_note or add_link targets a dropped unit, the drop is counted, and preflight on the resulting draft succeeds. Existing cases cover unknown_link_target inside the validator (:204-231, :396-403) and source_event_graph_open alone, never the combination.
  • Blank template: in extraction-graph (which already imports evidenceUnitMessages), Conversation mode with a whitespace systemPrompt yields the Conversation default; and getLtmExtractionConfig with a blank active template for conversation returns DEFAULT_LTM_EXTRACTION_PROMPT_CONVERSATION with activePromptTemplateId: null.
  • Also run extraction-reliability, candidate-reconciliation, routes-drafts and runtime.

Environment

  • Marinara Engine version: 2.5.0 line (staging 2039583ca)
  • Package version: long-term-memory 1.4.48 (upstream/staging 1f7954305)
  • Install type: source (code audit)
  • OS + version: not platform-specific
  • Browser or app shell: any

Logs, screenshots, or video

None. Found by the extraction stages audit (probes B and F, E1 reproductions) and confirmed by reading the code at 1f7954305.

Additional context

Tracker items EX-I1, EX-I2 and EX-I9. Shares no source file with the other batch-1 issues except the local-characters test, which only this issue edits. The follow-up EX-I5 (prompt envelope) should wait for this PR.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1448 Originally assigned to: @Promansis on GitHub. ## Summary Three extraction defects in `evidence-unit-extraction.ts`, filed together because they change the same file (tracker items EX-I2, EX-I1 and EX-I9): 1. **A provider-supplied `subjectKeys` value silently binds a fact to the wrong character.** The prompt hands the model every catalog key, tells it never to use them, and the server honours the field anyway. The resulting misattribution is only a warning, so it can auto-apply. 2. **A unit that links to a unit dropped after link closure compiles with a dangling link.** Low-risk auto-apply then fails the whole source at commit, and in manual review the mutation is blocked with a raw error the reviewer cannot repair. 3. **A whitespace-only prompt template makes Conversation and Game extraction run with the Roleplay prompt.** ## Affected package `long-term-memory` 1.4.48 (`upstream/staging` 1f7954305). - `S` = `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory` - `SH` = `packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory` ## Expected behavior 1. When the server resolves subjects from `subjectNames`, a `subjectKeys` value in the provider response is ignored, and the prompt catalog carries only names and aliases. 2. At the end of compilation, every `create_note` link target and `add_link` target is an existing note, the source note, or a note the same draft creates. A unit whose only grounding link pointed at a dropped unit is dropped too and counted as a validation rejection; a non-structural dangling link is stripped with a diagnostic. 3. A template that is empty after trimming falls back to that mode's default prompt. ## Actual behavior 1. Unit `subjectNames: ["Alice"]`, `subjectKeys: [<Bob's key>]`, text "Alice prefers tea over coffee" resolves to Bob with only a `subject_identity_normalized` warning. (Extraction stages audit, probe F.) 2. Four units where a `thread` with `claimKind: "change"` and no event link is dropped by `closeSourceEventGraph`: the `world_fact` that linked `involves -> thread_coffee_story` compiles as `create_note` with that link intact, `requiresReview: false`. Preflight then throws `Long-term memory draft link target not found: thread_coffee_story` and the source is reported `failed` at `finalize` with the fingerprint already persisted, so a retry reproduces it. (Probe B.) 3. A template whose prompt is only whitespace is active for Conversation mode: extraction runs with `DEFAULT_LTM_EXTRACTION_PROMPT` (the Roleplay text). All locations below were checked at 1f7954305. ## Steps to reproduce 1. **Wrong key.** Call `runLongTermMemoryEvidenceUnitExtraction` with a trusted catalog of Alice Vance and Bob Reyes and a provider response whose one `character_fact` unit names Alice and carries Bob's key. The mutation targets `char_bob_reyes`. 2. **Dangling link.** Compile the probe-B response (a `thread` unit with `claimKind: "change"` and no event link, plus a `world_fact` linking `involves` to it) with `applyLowRisk`. The draft commit throws at preflight. 3. **Blank template.** In Settings → Extraction prompts, make a template whose text is spaces only, set it active for Conversation, and extract a Conversation source with debug logging on: the request's system prompt is the Roleplay default. ## Minimal fix One PR, patch bump. **1. Ignore provider keys (`S/evidence-unit-extraction.ts`, `S/subject-identity.ts`)** - `normalizedEvidenceUnitRecord` (`:609-616`) only drops an *empty* `subjectKeys` array. When `resolveSubjectNames` is on, drop the field altogether; pass the flag in from the parser. The internal resolver still sets `subjectKeys` itself for established and borrowed keys (`S/subject-identity.ts:1279-1291`), so that path is unchanged. - `evidenceUnitResponseFormat` (`:422`, field at `:506-510`): omit `subjectKeys` from the provider JSON schema when `resolveSubjectNames` is on. - `trustedLtmSubjectPromptCatalog` (`S/subject-identity.ts:703-714`): stop serializing `key` and `ref`; the model is asked to copy names and aliases only. Check that nothing but the prompt reads this output. - The rule at `:1263` can keep its target-id half; the "database subject keys" half becomes moot. **2. Re-close links after late drops (`S/evidence-unit-extraction.ts`, compile section)** - Link closure runs once, inside `validateLtmEvidenceUnits` (`S/evidence-unit-validation.ts:392-442`). Three later passes drop units without re-closing: `deduplicateUnits` (`:1533`), `dropLocalCharactersOutsideRoleplay` (`:1534-1538`, `:1672`) and `closeSourceEventGraph` (`:1539`, `:1702`). The compiler copies unit links verbatim. - Fix: after `closeSourceEventGraph` (and again for `duplicateAliasClosure.units`, `:1560`), run the validator's link-closure step over the surviving units, with the valid-target set rebuilt from the survivors plus `existingNotes` and the source note, to a fixpoint with `closeSourceEventGraph`. Export that step from the validator rather than copying it. Strip a non-structural dangling link and emit a diagnostic; drop the unit when the dangling link was the one that satisfied relationship support or event grounding, with validator code `unknown_link_target`. Add the drops to `allDroppedCandidates` and `droppedCandidateCount` (`:1567-1581`) so the accounting still balances. - Live triggers at HEAD, for the fixture variants: a static unit linking to a `change` unit without an event link (probe B); a timeline event with `affects_character` to a character unit whose `claimKind` is `change` and that has no link back; any link to a local-character unit dropped outside Roleplay. **3. Blank template falls back by mode (`S/extraction-config.ts`, `S/evidence-unit-extraction.ts`)** - `getLtmExtractionConfig` (`S/extraction-config.ts:50-58`) returns `template.prompt` untrimmed; treat a template whose trimmed prompt is empty as absent, so the existing fallback at `:59-62` picks the mode default. - `evidenceUnitMessages` (`:1160-1161`) falls back to `DEFAULT_LTM_EXTRACTION_PROMPT`; use `DEFAULT_LTM_EXTRACTION_PROMPTS_BY_MODE[options.mode ?? options.modes[0] ?? "roleplay"]`, the same expression `:1530` uses for `allowedBuckets`. - Do **not** tighten `SH/schema.ts:177` (`prompt: z.string().min(1)`) with `.trim()`: stored settings that already hold a blank template would then fail to parse on load. **CHANGELOG under `## Unreleased`**: one bullet each for 1, 2 and 3, written for users (which character a fact is saved to; imports no longer fail or block review on a dropped link; blank templates). **Not included** - Resolving link targets outside the retrieval window (audit F7, part 2) and the prompt-envelope work (F5, F6, tracker EX-I5). EX-I5 touches the same file and should follow this PR. - A dedicated drop reason for `unknown_link_target`; the message fix is tracker EX-I6, in a separate issue. ## Regression proof - **`tests/long-term-memory-local-characters.regression.ts`** (already exercises `subjectKeys`, `:207-333`, `:716-722`): the probe-F pair resolves to Alice with and without the wrong key; the serialized `trustedSubjects` carries no `key` or `ref`; the established-key and borrowed-key cases still pass. Cases that model provider output and expect a provider key to win change to expect name resolution. - **`tests/long-term-memory-extraction-graph.regression.ts`**: the probe-B fixture plus the `affects_character` and local-character variants. Assert that no compiled `create_note` or `add_link` targets a dropped unit, the drop is counted, and `preflight` on the resulting draft succeeds. Existing cases cover `unknown_link_target` inside the validator (`:204-231`, `:396-403`) and `source_event_graph_open` alone, never the combination. - **Blank template**: in `extraction-graph` (which already imports `evidenceUnitMessages`), Conversation mode with a whitespace `systemPrompt` yields the Conversation default; and `getLtmExtractionConfig` with a blank active template for `conversation` returns `DEFAULT_LTM_EXTRACTION_PROMPT_CONVERSATION` with `activePromptTemplateId: null`. - Also run `extraction-reliability`, `candidate-reconciliation`, `routes-drafts` and `runtime`. ## Environment - Marinara Engine version: 2.5.0 line (`staging` 2039583ca) - Package version: long-term-memory 1.4.48 (`upstream/staging` 1f7954305) - Install type: source (code audit) - OS + version: not platform-specific - Browser or app shell: any ## Logs, screenshots, or video None. Found by the extraction stages audit (probes B and F, E1 reproductions) and confirmed by reading the code at 1f7954305. ## Additional context Tracker items EX-I1, EX-I2 and EX-I9. Shares no source file with the other batch-1 issues except the `local-characters` test, which only this issue edits. The follow-up EX-I5 (prompt envelope) should wait for this PR. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:30:11 +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#734
No description provided.