fix(memos-local-plugin): salvage partially structured L3 abstraction drafts
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 stringl3.abstraction: 'inference' must be an arrayl3.abstraction: 'constraints' must be an array
The wire shapes that triggered this in the wild:
titleempty /null/ non-stringinference/constraintsreturned 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_tagsreturned 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/valueinstead ofdescription, andname/title/heading/keyinstead oflabel evidence_ids/evidencealiases (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:
- Cleaned LLM-provided title
- First non-empty
inference->environment->constraintsentry's label or description - First non-heading line of
body - Joined
domain_tags - 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_failedonly 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_tagsstring 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, addnormaliseDraftsalvaging, soft floor, observability log)apps/memos-local-plugin/tests/unit/memory/l3/abstract.test.ts(rewrite the missing-triple expectation, add 7 salvage tests)
Thanks — addressed all four in commit e3142de:
abstract.ts:515(collectEvidenceIdsarray branch): Now trims each entry and drops empties so leading/trailing whitespace from providers like"po_1 "no longer breaksstartsWith("po_")evidence-chip classification. Matches the existing string-branch behavior.abstract.ts:481+buildBody(empty bold labels): Extracted arenderEntryhelper that emits- <description>whenlabel === ""instead of- **** — <description>. Applied across environment / inference / constraints lists.abstract.ts:306(deriveTitledoc): Updated to match the actual behavior: "first non-empty markdown line ofbody, with leading heading/list prefixes (#,-,*,+,1.) stripped, trimmed."abstract.ts:560(normaliseTagsdoc): 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.
This PR has been automatically marked as stale due to inactivity.