MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

fix(memos-local-plugin): salvage partially structured L3 abstraction drafts

Open Sanjays2402 opened this issue 6 months ago • 2 comments

Closes #1668

Problem

apps/memos-local-plugin aborted world_model_generate whenever the LLM returned a draft that was slightly off-schema, even when the payload was clearly salvageable. On smaller / non-strict structured-output providers (e.g. glm-4-flashx-250414), the L3 pipeline failed hard with errors like:

  • l3.abstraction: 'title' must be a non-empty string
  • l3.abstraction: 'inference' must be an array
  • l3.abstraction: 'constraints' must be an array

The wire shapes that triggered this in the wild:

  • title empty / null / non-string
  • inference / constraints returned as a string or single object instead of an array
  • list entries returned as plain strings (e.g. ["foo"]) or { body: "..." } shapes instead of { label, description }
  • domain_tags returned as a comma-joined string

Fix

The inline validate callback in core/memory/l3/abstract.ts was removed; instead, normaliseDraft now salvages the common malformed wire shapes and a soft floor check rejects only fully-empty drafts.

Entry coercion (toEntries / coerceEntry)

Now accepts:

  • arrays (canonical)
  • a single string -> one description-only entry
  • a single object -> wrapped in an array
  • per-entry strings -> { label: "", description: <string> }
  • per-entry objects keyed by body / text / content / detail / summary / value instead of description, and name / title / heading / key instead of label
  • evidence_ids / evidence aliases (including comma-joined strings)

Tag coercion (normaliseTags)

Now accepts:

  • arrays of strings (canonical)
  • comma / semicolon / newline-joined strings ("a, b, c" -> ["a","b","c"])
  • arrays of { label | name | tag | value | key } objects

Title derivation (deriveTitle)

When the LLM omits a usable title, derive one in this order:

  1. Cleaned LLM-provided title
  2. First non-empty inference -> environment -> constraints entry's label or description
  3. First non-heading line of body
  4. Joined domain_tags
  5. Empty (soft floor will reject if everything else is also empty)

Soft floor (assertDraftMinimallyUsable)

If, after normalization, the draft has no title, no triple entries, no body, and no domain tags, we still throw LLM_OUTPUT_MALFORMED so we don't index garbage. Downstream validators continue to get the final say on whether a salvaged draft is good enough to persist.

Observability

When the parser had to coerce the wire format, an l3.abstract.draft_salvaged info log captures which keys were non-canonical, so operators can spot providers that consistently need salvaging.

Tests

All 12 tests in tests/unit/memory/l3/abstract.test.ts pass (5 original + 7 new):

  • salvages missing triple into an empty-but-titled draft instead of failing
  • returns llm_failed only when even normalisation can't recover anything
  • salvages string list entries into description-only entries
  • salvages {body: ...} list entries to canonical {label, description}
  • derives a title from inference when the LLM left it blank
  • falls back to domain tags when title and triple are unhelpful
  • splits a comma-joined domain_tags string into an array

The pre-existing test for "returns llm_failed when the LLM returns missing triple" was rewritten as "salvages missing triple into an empty-but-titled draft" because that behavior is exactly the policy the issue asks us to flip: a draft with a usable title should not be discarded just because the triple is missing.

tsc -p tsconfig.json --noEmit passes. The remaining failures in the broader unit suite (reward/*, storage/migrator, memory/l2/gain, memory/l3/cluster, pipeline/memory-core) reproduce on plain main without these changes and are unrelated to this PR.

Files Touched

  • apps/memos-local-plugin/core/memory/l3/abstract.ts (drop strict inline validate, add normaliseDraft salvaging, soft floor, observability log)
  • apps/memos-local-plugin/tests/unit/memory/l3/abstract.test.ts (rewrite the missing-triple expectation, add 7 salvage tests)

Sanjays2402 avatar May 09 '26 19:05 Sanjays2402

Thanks — addressed all four in commit e3142de:

  • abstract.ts:515 (collectEvidenceIds array branch): Now trims each entry and drops empties so leading/trailing whitespace from providers like "po_1 " no longer breaks startsWith("po_") evidence-chip classification. Matches the existing string-branch behavior.
  • abstract.ts:481 + buildBody (empty bold labels): Extracted a renderEntry helper that emits - <description> when label === "" instead of - **** — <description>. Applied across environment / inference / constraints lists.
  • abstract.ts:306 (deriveTitle doc): Updated to match the actual behavior: "first non-empty markdown line of body, with leading heading/list prefixes (#, -, *, +, 1.) stripped, trimmed."
  • abstract.ts:560 (normaliseTags doc): Tightened to "comma/semicolon/newline-separated strings" + a note that intra-tag whitespace is preserved (multi-word tags survive).

Tests: added "trims and drops empty evidence ids from array entries" and "renders string-only entries without an empty bold label in body". 14/14 in abstract.test.ts pass; tsc -p tsconfig.json --noEmit clean.

Sanjays2402 avatar May 09 '26 20:05 Sanjays2402

This PR has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Jun 09 '26 06:06 github-actions[bot]