{"record":{"id":"e065285251c49251","repo":"shareAI-lab/learn-claude-code","slug":"the-memory-index-is-not-a-memory-record","errorCode":null,"errorMessage":"The memory index is not a memory record","messagePattern":"The memory index is not a memory record","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"s09_memory/code.py","lineNumber":92,"sourceCode":"    if len(parts) < 3:\n        return {}, text\n    try:\n        metadata = yaml.safe_load(parts[1]) or {}\n    except yaml.YAMLError:\n        return {}, text\n    if not isinstance(metadata, dict):\n        return {}, text\n    return metadata, parts[2].lstrip()\n\ndef memory_slug(name: str) -> str:\n    slug = re.sub(r\"[^\\w]+\", \"-\", name.lower()).strip(\"-_\")\n    return slug or \"memory\"\n\ndef memory_path(filename: str, allow_index: bool = False) -> Path:\n    if Path(filename).name != filename:\n        raise ValueError(f\"Invalid memory filename: {filename}\")\n    if filename == MEMORY_INDEX.name and not allow_index:\n        raise ValueError(\"The memory index is not a memory record\")\n\n    root = MEMORY_DIR.resolve()\n    if not root.is_relative_to(WORKDIR.resolve()):\n        raise ValueError(\"Memory directory escapes the workspace\")\n    path = (root / filename).resolve()\n    if not path.is_relative_to(root):\n        raise ValueError(f\"Memory path escapes the store: {filename}\")\n    return path\n\ndef _memory_slug(name: str) -> str:\n    return memory_slug(name)\n\ndef _normalized_memory_text(value: str) -> str:\n    return \" \".join(value.lower().split())\n\ndef should_store_memory(candidate: dict, existing: list[dict]) -> bool:\n    \"\"\"Accept durable records that are not temporary or already stored.\"\"\"\n    if not isinstance(candidate, dict):","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s09_memory/code.py#L74-L110","documentation":"Raised by memory_path() in s09_memory/code.py when the caller passes the memory index file's name (MEMORY_INDEX.name, e.g. INDEX.md) without allow_index=True. The index is a generated catalog of memory records, not a record itself, so record-oriented tools (read/write/delete a memory) refuse to operate on it to prevent corruption or recursive self-reference. Only the dedicated index-rebuild path passes allow_index=True.","triggerScenarios":"A memory tool call with filename equal to the index file's name; listing or globbing MEMORY_DIR and feeding every resulting filename back into a per-record tool without filtering out the index; a memory slug that happens to collide with the index filename.","commonSituations":"Agent iterating directory contents and treating every file as a record; user-visible index file tempting direct edits; slug collisions when memory names sanitize to 'index'.","solutions":["Filter the index filename out of any directory listing before per-record calls","Choose memory names that do not slug down to the index filename","Only the index-maintenance code path should pass allow_index=True; never expose that flag through the agent tool surface"],"exampleFix":"// before\nfor f in MEMORY_DIR.iterdir():\n    memory_read(f.name)  # blows up on INDEX file\n// after\nfor f in MEMORY_DIR.iterdir():\n    if f.name != MEMORY_INDEX.name:\n        memory_read(f.name)","handlingStrategy":"validation","validationCode":"INDEX_NAME = MEMORY_INDEX.name  # e.g. 'INDEX.md'\nrecords = [f for f in MEMORY_DIR.iterdir() if f.name != INDEX_NAME]\nfor f in records:\n    memory_read(f.name)","typeGuard":"def is_memory_record(filename: str) -> bool:\n    return filename != MEMORY_INDEX.name and Path(filename).name == filename","tryCatchPattern":"try:\n    memory_path(fn)\nexcept ValueError as e:\n    if 'index is not a memory record' in str(e):\n        skip = True  # it's the catalog, not a record\n    else:\n        raise","preventionTips":["Always filter the index file out of directory scans before per-record operations","Never expose allow_index=True through the agent tool surface","Avoid memory names whose slug collides with the index filename"],"tags":["memory","reserved-resource","validation"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}