[GH-ISSUE #1416] [Issue]: Long-Term Memory server: six core modules import each other in a loop because of one 3-line helper #720

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

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

Originally assigned to: @Promansis on GitHub.

Summary

Six core server modules of Long-Term Memory form a runtime import cycle. Storage, drafts, rejected suggestions, subject identity, the draft projector and scoped targets all reach each other. Nothing breaks today, but a cycle makes module start-up order fragile: any module-level use of an imported value can fail with "Cannot access … before initialization", depending on which module loads first. It also stops any one of these modules being loaded or tested on its own.

The whole cycle hangs on one pure 3-line helper, subjectsEqual. Moving it to its own module removes the cycle.

Affected package

long-term-memory 1.4.38 (upstream/staging 97c0486f8), under packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/. The runtime imports that form the cycle (type-only imports excluded):

Module Imports from the cycle
storage.ts isAdditiveLtmSection from draft-projector.ts
draft-projector.ts canUpdateLtmScopedTarget from scoped-targets.ts; subjectsEqual from subject-identity.ts (:26)
scoped-targets.ts subjectsEqual from subject-identity.ts (:20)
subject-identity.ts LongTermMemoryStorage from storage.ts; readSavedLtmSubjectIdentityChoices from rejected-suggestions.ts
rejected-suggestions.ts draftPathForId from draft-store.ts
draft-store.ts LongTermMemoryStorage from storage.ts

subjectsEqual (subject-identity.ts:2139-2142) compares two subject lists by key. It is the only thing draft-projector.ts and scoped-targets.ts take from the 2,740-line subject-identity.ts.

Expected behavior

The LTM server folder has no runtime import cycles.

Actual behavior

A strongly-connected-component scan of every runtime import in the server folder finds exactly one cycle, with all six modules above in it. Moving subjectsEqual to a new leaf module and pointing the two importers at it leaves zero cycles. This was checked on a throwaway copy, outside the repository.

Steps to reproduce

  1. List the local imports of the six files above.
  2. Follow them: storage → draft-projector → subject-identity → storage, and subject-identity → rejected-suggestions → draft-store → storage → draft-projector → scoped-targets → subject-identity.

Environment

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

Logs, screenshots, or video

Not applicable.

Minimal fix

  1. Move subjectsEqual from subject-identity.ts to a new subject-equality.ts in the same folder. It depends only on the LtmSubject type.
  2. Import it from there in draft-projector.ts, scoped-targets.ts, evidence-unit-compiler.ts, source-extraction.ts and subject-identity.ts.
  3. No behaviour change. It's a one-function move, so no dedicated test; the build and the existing LTM regressions prove it.

Validation:

  • npm run check
  • node scripts/typecheck-packages.mjs long-term-memory
  • rebuild LTM (server.mjs changes; patch bump)
  • node scripts/validate-catalog.mjs
  • the storage, candidate-reconciliation and extraction-graph regressions

Additional context

  • Out of scope: the other layering notes from the 1.4.8 architecture audit:

    • storage reading draft files directly;
    • the rejected-suggestions file also storing identity decisions;
    • splitting subject-identity.ts.

    Each needs its own design decision.

  • Optional follow-up: nothing stops a new cycle from forming. A repo-wide cycle check would belong in scripts/ and is a separate decision.

  • Timing: subject-identity.ts changes often. Land this between batches to avoid merge conflicts.

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 15, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1416 Originally assigned to: @Promansis on GitHub. ## Summary Six core server modules of Long-Term Memory form a runtime import cycle. Storage, drafts, rejected suggestions, subject identity, the draft projector and scoped targets all reach each other. Nothing breaks today, but a cycle makes module start-up order fragile: any module-level use of an imported value can fail with "Cannot access … before initialization", depending on which module loads first. It also stops any one of these modules being loaded or tested on its own. The whole cycle hangs on one pure 3-line helper, `subjectsEqual`. Moving it to its own module removes the cycle. ## Affected package `long-term-memory` 1.4.38 (`upstream/staging` 97c0486f8), under `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/`. The runtime imports that form the cycle (type-only imports excluded): | Module | Imports from the cycle | |---|---| | `storage.ts` | `isAdditiveLtmSection` from `draft-projector.ts` | | `draft-projector.ts` | `canUpdateLtmScopedTarget` from `scoped-targets.ts`; `subjectsEqual` from `subject-identity.ts` (`:26`) | | `scoped-targets.ts` | `subjectsEqual` from `subject-identity.ts` (`:20`) | | `subject-identity.ts` | `LongTermMemoryStorage` from `storage.ts`; `readSavedLtmSubjectIdentityChoices` from `rejected-suggestions.ts` | | `rejected-suggestions.ts` | `draftPathForId` from `draft-store.ts` | | `draft-store.ts` | `LongTermMemoryStorage` from `storage.ts` | `subjectsEqual` (`subject-identity.ts:2139-2142`) compares two subject lists by key. It is the only thing `draft-projector.ts` and `scoped-targets.ts` take from the 2,740-line `subject-identity.ts`. ## Expected behavior The LTM server folder has no runtime import cycles. ## Actual behavior A strongly-connected-component scan of every runtime import in the server folder finds exactly one cycle, with all six modules above in it. Moving `subjectsEqual` to a new leaf module and pointing the two importers at it leaves zero cycles. This was checked on a throwaway copy, outside the repository. ## Steps to reproduce 1. List the local imports of the six files above. 2. Follow them: storage → draft-projector → subject-identity → storage, and subject-identity → rejected-suggestions → draft-store → storage → draft-projector → scoped-targets → subject-identity. ## Environment - Marinara Engine version: 2.5.0 line (`staging`) - Package version: long-term-memory 1.4.38 (`upstream/staging` 97c0486f8) - Install type: source (code audit) - OS + version: not platform-specific - Browser or app shell: any ## Logs, screenshots, or video Not applicable. ## Minimal fix 1. Move `subjectsEqual` from `subject-identity.ts` to a new `subject-equality.ts` in the same folder. It depends only on the `LtmSubject` type. 2. Import it from there in `draft-projector.ts`, `scoped-targets.ts`, `evidence-unit-compiler.ts`, `source-extraction.ts` and `subject-identity.ts`. 3. No behaviour change. It's a one-function move, so no dedicated test; the build and the existing LTM regressions prove it. Validation: - `npm run check` - `node scripts/typecheck-packages.mjs long-term-memory` - rebuild LTM (`server.mjs` changes; patch bump) - `node scripts/validate-catalog.mjs` - the storage, candidate-reconciliation and extraction-graph regressions ## Additional context - **Out of scope:** the other layering notes from the 1.4.8 architecture audit: - storage reading draft files directly; - the rejected-suggestions file also storing identity decisions; - splitting `subject-identity.ts`. Each needs its own design decision. - **Optional follow-up:** nothing stops a new cycle from forming. A repo-wide cycle check would belong in `scripts/` and is a separate decision. - **Timing:** `subject-identity.ts` changes often. Land this between batches to avoid merge conflicts. ## 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:03 +07:00
  • closed this issue
  • added the
    bug
    label
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#720
No description provided.