{"record":{"id":"6092e754d8b5aee5","repo":"shareAI-lab/learn-claude-code","slug":"unknown-memory-type-mem-type","errorCode":null,"errorMessage":"Unknown memory type: {mem_type}","messagePattern":"Unknown memory type: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"s09_memory/code.py","lineNumber":153,"sourceCode":"        ) == normalized_description:\n            return False\n        if _normalized_memory_text(str(memory.get(\"body\", \"\"))) == normalized_body:\n            return False\n    return True\n\ndef memory_document(name: str, mem_type: str, description: str, body: str) -> str:\n    metadata = yaml.safe_dump(\n        {\"name\": name, \"description\": description, \"type\": mem_type},\n        sort_keys=False,\n        allow_unicode=True,\n    ).strip()\n    return f\"---\\n{metadata}\\n---\\n\\n{body.strip()}\\n\"\n\ndef write_memory_file(name: str, mem_type: str, description: str, body: str) -> Path:\n    if not name.strip():\n        raise ValueError(\"Memory name cannot be empty\")\n    if mem_type not in MEMORY_TYPES:\n        raise ValueError(f\"Unknown memory type: {mem_type}\")\n    if not description.strip() or not body.strip():\n        raise ValueError(\"Memory description and body cannot be empty\")\n\n    MEMORY_DIR.mkdir(parents=True, exist_ok=True)\n    path = memory_path(f\"{memory_slug(name)}.md\")\n    path.write_text(memory_document(name, mem_type, description, body))\n    rebuild_memory_index()\n    return path\n\ndef rebuild_memory_index() -> None:\n    MEMORY_DIR.mkdir(parents=True, exist_ok=True)\n    lines = []\n    for path in sorted(MEMORY_DIR.glob(\"*.md\")):\n        if path.name == MEMORY_INDEX.name:\n            continue\n        try:\n            path = memory_path(path.name)\n        except ValueError:","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s09_memory/code.py#L135-L171","documentation":"Raised by write_memory_file() in s09_memory/code.py:153 when mem_type is not one of the allowed MEMORY_TYPES: 'user', 'feedback', 'project', 'reference'. Memory records are tagged with a type in their YAML front matter and the index/rebuild logic groups by it, so unknown types would silently fragment the store. The membership test is an exact, case-sensitive tuple check, so 'User' or 'USER' fail just like 'preference'.","triggerScenarios":"Calling write_memory_file(name, 'preference', ...) — 'preference' is not in the tuple; passing a capitalized variant like 'Feedback'; passing a type string obtained from an LLM response that invented a new category; passing None or a non-string.","commonSituations":"LLM-driven memory capture where the model free-forms a type outside the allowed vocabulary; hand-written scripts that assume an intuitive-but-wrong type name; version drift if you upgrade code that once accepted arbitrary types.","solutions":["Use one of the exact allowed values: 'user', 'feedback', 'project', 'reference' (import MEMORY_TYPES and choose from it).","Map free-form or capitalized types to the closest allowed type before writing, defaulting to 'reference'.","If a genuinely new category is needed, extend the MEMORY_TYPES tuple deliberately and rebuild the index."],"exampleFix":"# before\nwrite_memory_file(name, 'preference', description, body)\n\n# after\nfrom s09_memory.code import MEMORY_TYPES\nmem_type = mem_type.lower() if mem_type in MEMORY_TYPES else 'reference'\nwrite_memory_file(name, mem_type, description, body)","handlingStrategy":"type-guard","validationCode":"from s09_memory.code import MEMORY_TYPES\n\ndef pick_memory_type(candidate: str) -> str:\n    return candidate if candidate in MEMORY_TYPES else 'reference'","typeGuard":"def is_known_memory_type(mem_type) -> bool:\n    from s09_memory.code import MEMORY_TYPES\n    return isinstance(mem_type, str) and mem_type in MEMORY_TYPES","tryCatchPattern":"try:\n    write_memory_file(name, mem_type, description, body)\nexcept ValueError as e:\n    if 'Unknown memory type' in str(e):\n        write_memory_file(name, 'reference', description, body)\n    else:\n        raise","preventionTips":["Import MEMORY_TYPES and choose values from it instead of hardcoding strings.","Normalize LLM-suggested types through an explicit mapping to the four allowed values.","Extend MEMORY_TYPES deliberately, never by catching the error and writing anyway."],"tags":["validation","memory","enums"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}