fix(memory): resolve partial IDs beyond the first 1000 records (#4389)

## Description

`headroom memory show` tries a full ID first, then searches by prefix.
The fallback only examined the newest 1,000 memories. That could report
an older memory as missing or silently select a newer memory when
another matching ID existed outside that first page.

Use the existing unlimited `MemoryFilter()` query for this fallback.
Exact-ID lookup, literal prefix matching, superseded-memory filtering,
and the existing missing/ambiguous-ID errors stay the same.

## Type of Change

- [x] Bug fix

## Changes Made

- Remove the 1,000-row cap from the partial-ID fallback.
- Add real SQLite CLI regressions for an older unique match and
ambiguity across that boundary.

## Testing

- [x] Focused and affected integration tests pass.
- [x] Lint, format, type, and repository pre-push checks pass.
- [x] Actual CLI behavior verified with a synthetic SQLite database.

- Before the correction, `TestMemoryShow` had 4 passing tests and both
new regressions failed.
- Afterward, all 6 show tests passed.
- All 77 affected CLI, index-sync, query-filter, and SQLite-store tests
passed.
- Whole-project Ruff check and format check passed (1,812 files already
formatted).
- Repository pre-commit checks, including mypy and commitlint, passed.
- The full repository pre-push gate passed under pinned Rust 1.95.0 and
Node 22.20.0: Rust formatting, workspace Clippy/tests, extension
rebuild/import verification, 177 Python tests (4 skipped), and
commitlint. No hooks were bypassed.
- Independent review found no blocking findings.

```text
77 passed, 274 warnings in 2.21s
```

The warnings are existing `datetime.utcnow()` deprecations.

## Real Behavior Proof

- Environment: macOS arm64, Python 3.12.15, Rust 1.95.0, editable
checkout with its native extension built locally. The 77 affected tests
and CLI proof were repeated after the pinned extension rebuild. No
provider/model was used for this proof. All proof data was synthetic,
with offline mode enabled and telemetry disabled.

- Exact command / steps: Create a temporary `SQLiteMemoryStore`, use
`save_batch` to add 1,000 filler memories dated 2022-01-01 and
`target-memory-old` dated 2021-01-01. Run the actual installed CLI
against that database:

```sh
headroom memory show --db-path "$repro_db" target-memory-old --json
headroom memory show --db-path "$repro_db" target- --json
headroom memory show --db-path "$repro_db" absent- --json
```

- Observed result: The unique older prefix resolves, the missing ID is
rejected, and adding a newer match makes the shared prefix ambiguous:

```text
target-memory-old: exit 0, id=target-memory-old
target-:          exit 0, id=target-memory-old
absent-:          exit 1, Error: Memory not found: absent-
```

Then add `target-memory-new` dated 2023-01-01 and repeat the `target-`
command:

```text
exit 1, Error: Ambiguous ID 'target-'. Matches: ['target-m', 'target-m']
```

On the unchanged baseline, the unique older prefix instead returned
`Memory not found`; after adding the newer match, the same prefix
incorrectly exited 0 and selected `target-memory-new`. Full-ID and
missing-ID controls behaved correctly before and after. The temporary
database and all CLI child processes were cleaned up.

- Not tested: The complete Python suite, external providers or embedding
models, Windows, or Linux. The prefix fallback still materializes the
matching store's full query result; full IDs retain the direct lookup
path.

## Runtime Rollout Safety

- Rollout-managed feature(s): None. This is a local SQLite CLI
correction.
- Minimum rollout channel: Not applicable; no rollout-managed feature or
channel changes.
- Stable/default behavior changed: Yes. Partial-ID lookup searches all
eligible memories and rejects ambiguous prefixes across the former
1,000-row boundary.
- Kill switch / disable path: No new switch. Full IDs retain the direct
lookup path.
- Unsafe override required: No.
- Qualification impact: No schema, configuration, dependency, provider,
or rollout-qualification changes.
- Rollback path: Revert commit 017c9a5 to restore the previous behavior.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

The contributing agent completed self-review and verification; a
separate reviewer checked the complete patch.

## Checklist

- [x] Regression tests fail before the fix and pass afterward.
- [x] Focused and affected integration tests pass.
- [x] No manual changes to `CHANGELOG.md`.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4389"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
Ehsan-hash
2026-10-24 08:44:26 -07:00
committed by GitHub
co-authored by JerrettDavis
parent 19c91a870f
commit c4ea31b156
2 changed files with 74 additions and 1 deletions
+1 -1
View File
@@ -598,7 +598,7 @@ def show_memory(
# If not found with exact ID, try partial match
if mem is None:
# Query all and filter by prefix
all_memories = asyncio.run(store.query(MemoryFilter(limit=1000)))
all_memories = asyncio.run(store.query(MemoryFilter()))
matches = [m for m in all_memories if m.id.startswith(memory_id)]
if len(matches) == 0:
+73
View File
@@ -165,6 +165,79 @@ class TestMemoryShow:
assert result.exit_code == 0
assert "TypeScript" in result.output
def test_show_by_partial_id_beyond_default_query_limit(
self, runner: CliRunner, temp_db: str
) -> None:
"""Show a unique partial ID when the match is older than 1,000 rows."""
store = SQLiteMemoryStore(temp_db)
filler_start = datetime(2024, 1, 1)
memories = [
Memory(
id=f"filler-{index:04d}",
content=f"Filler memory {index}",
user_id="test-user",
created_at=filler_start + timedelta(days=index),
valid_from=filler_start + timedelta(days=index),
)
for index in range(1000)
]
memories.append(
Memory(
id="legacy-target-memory",
content="Legacy target outside the first page",
user_id="test-user",
created_at=datetime(2020, 1, 1),
valid_from=datetime(2020, 1, 1),
)
)
asyncio.run(store.save_batch(memories))
result = runner.invoke(main, ["memory", "show", "--db-path", temp_db, "legacy-target-"])
assert result.exit_code == 0
assert "Legacy target outside the first page" in result.output
def test_show_by_partial_id_reports_ambiguity_beyond_default_query_limit(
self, runner: CliRunner, temp_db: str
) -> None:
"""Report ambiguity when matching IDs span beyond the first 1,000 rows."""
store = SQLiteMemoryStore(temp_db)
filler_start = datetime(2024, 1, 1)
memories = [
Memory(
id=f"filler-{index:04d}",
content=f"Filler memory {index}",
user_id="test-user",
created_at=filler_start + timedelta(days=index),
valid_from=filler_start + timedelta(days=index),
)
for index in range(1000)
]
memories.extend(
[
Memory(
id="shared-prefix-old",
content="Older matching memory",
user_id="test-user",
created_at=datetime(2020, 1, 1),
valid_from=datetime(2020, 1, 1),
),
Memory(
id="shared-prefix-new",
content="Newer matching memory",
user_id="test-user",
created_at=datetime(2026, 1, 1),
valid_from=datetime(2026, 1, 1),
),
]
)
asyncio.run(store.save_batch(memories))
result = runner.invoke(main, ["memory", "show", "--db-path", temp_db, "shared-prefix-"])
assert result.exit_code != 0
assert "Ambiguous ID 'shared-prefix-'" in result.output
def test_show_json_output(self, runner: CliRunner, populated_db: str) -> None:
"""Show memory as JSON."""
result = runner.invoke(