omi icon indicating copy to clipboard operation
omi copied to clipboard

Harden developer memories list against malformed records

Open tianmind-studio opened this issue 5 months ago • 1 comments

Summary

  • Make CleanerMemory tolerate legacy Developer API memory records with missing/invalid optional fields by coercing safe defaults.
  • Keep the existing per-record validation guard from main, while skipping non-dict or id-less records before response serialization.
  • Add HTTP coverage for paginated /v1/dev/user/memories responses containing malformed legacy records so a single bad record does not turn the page into a 500.

Regression check

With the new tests but backend/routers/developer.py temporarily restored to origin/main:

  • python -m pytest tests\unit\test_dev_api_folder_filters.py -q -> 2 failed, 16 passed, 2 warnings
  • failures covered a 500 response from /v1/dev/user/memories?limit=3&offset=7 and direct CleanerMemory(...) validation errors for legacy values

Testing

  • python -m pytest tests\unit\test_dev_api_folder_filters.py -q -> 18 passed, 2 warnings
  • python -m black --line-length 120 --skip-string-normalization routers\developer.py tests\unit\test_dev_api_folder_filters.py --check
  • python -m py_compile routers\developer.py tests\unit\test_dev_api_folder_filters.py
  • git diff --check origin/main...HEAD

tianmind-studio avatar Jun 10 '26 08:06 tianmind-studio

Greptile Summary

This PR hardens the GET /v1/dev/user/memories Developer API endpoint so that a single malformed or legacy Firestore record no longer turns an entire paginated response into a 500. It adds per-field field_validator coercions to CleanerMemory (covering None ids, invalid categories, bad datetimes, non-list tags, stringified booleans, etc.) and a pre-filter that skips non-dict or id-less records before model_validate is called.

  • CleanerMemory now tolerates every previously-required field being absent or of the wrong type by coercing safe defaults, mirroring the hardening already present on the /v3/memories path.
  • A pre-filter guard (isinstance(memory, dict) and memory.get('id')) plus a try/except ValidationError loop replace the previous direct append, preventing any single bad record from bubbling up as an unhandled exception.
  • New HTTP-layer and unit tests in TestDevApiMemoriesHttpLayer cover a mixed-quality page (good record, legacy malformed record, locked record with null content, id-less record) and directly exercise each validator's edge values.

Confidence Score: 4/5

Safe to merge; the changes are additive hardening on a read path and the new validators are well-tested.

The coercion logic is thorough and the tests confirm the key failure scenarios. Two minor quality gaps exist: the coerce_datetime numeric branch silently delegates to Pydantic's internal timestamp coercion rather than explicitly calling datetime.fromtimestamp, and coerce_id allows an empty-string id to propagate through CleanerMemory when the model is used directly (e.g., as the PATCH response model) without the endpoint's pre-filter guarding it.

backend/routers/developer.py — specifically the coerce_id and coerce_datetime validators.

Important Files Changed

Filename Overview
backend/routers/developer.py Adds field_validator coercions to CleanerMemory so legacy/malformed Firestore records survive serialization; adds a pre-filter that skips non-dict or id-less records before model_validate; fixes a potential None[:70] crash in the locked-memory content truncation path.
backend/tests/unit/test_dev_api_folder_filters.py Adds TestDevApiMemoriesHttpLayer with an HTTP-layer test that exercises a mixed-quality page (good record, legacy malformed record, locked record with null content, id-less record) and a direct unit test for CleanerMemory validator edge values; both tests are clean and well-structured.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[GET /v1/dev/user/memories] --> B[memories_db.get_memories]
    B --> C{For each record}
    C --> D{is dict AND has id?}
    D -- No --> E[Skip with warning]
    D -- Yes --> F{is_locked?}
    F -- Yes --> G[Truncate content to 70 chars]
    G --> H[CleanerMemory.model_validate]
    F -- No --> H
    H --> I{ValidationError?}
    I -- No --> J[Append to valid_memories]
    I -- Yes --> K[Skip with warning]
    J --> L[Return valid_memories]

Reviews (1): Last reviewed commit: "Make dev memories list tolerate malforme..." | Re-trigger Greptile

greptile-apps avatar Jun 10 '26 09:06 greptile-apps

Refreshed this branch onto current main (f05687ae) and re-ran the Developer API memory regression checks from the repo root on Windows.

Validation:

  • python -m pytest backend\tests\unit\test_dev_api_folder_filters.py backend\tests\unit\test_dev_api_memories_pagination.py -q --tb=short -> 22 passed, 2 warnings
  • python -m black --check --line-length 120 --skip-string-normalization backend\routers\developer.py backend\tests\unit\test_dev_api_folder_filters.py backend\tests\unit\test_dev_api_memories_pagination.py
  • python -m py_compile backend\routers\developer.py backend\tests\unit\test_dev_api_folder_filters.py backend\tests\unit\test_dev_api_memories_pagination.py
  • git diff --check origin/main..HEAD and git diff --check
  • scripts/pre-commit

tianmind-studio avatar Jun 14 '26 06:06 tianmind-studio

ClawSweeper local pilot review

Recommendation: keep open for manual maintainer review.

What it found:

  • Current main has the related skip-and-log fix for malformed developer memory records.
  • This PR adds a separate follow-up field-coercion layer for recoverable legacy records.
  • The main merge risk is response-contract compatibility: nullable/defaulted fields may now be returned where current main skips malformed records.
  • Real Developer API behavior proof is still needed.

Related context: partial overlap with #7678, but not fully superseded by it.

Suggested next step: maintainers should explicitly accept or document the nullable/defaulted response behavior, then require redacted real API proof before merge.

Posted from a local report-only ClawSweeper pilot by request; no labels, closes, repairs, or merges were performed.

ThomsenDrake avatar Jun 17 '26 00:06 ThomsenDrake