Skip to content
Python

fix(memhub): a hand-added memory no longer waits Pending forever

alvinfeif3d13c4351fd
diff --git a/memhub/apps/core/src/one_memory/api.py b/memhub/apps/core/src/one_memory/api.py
index 97338be..de06478 100644
--- a/memhub/apps/core/src/one_memory/api.py
+++ b/memhub/apps/core/src/one_memory/api.py
@@ -1026,9 +1026,7 @@ def create_app(
app.state.memory_write_intake = UnifiedWriteIntake(
memory_write_queue,
- source_writes_permitted=lambda source_id: (
- source_id == "one-memory-manual" or _source_writes_on(app, source_id)
- ),
+ source_writes_permitted=lambda source_id: _source_writes_on(app, source_id),
)
app.state.start_memory_backfill = lambda: _start_backfill(app)
@@ -4665,10 +4663,17 @@ def _evidence_store(app: FastAPI) -> EvidenceStore:
def _source_writes_on(app: FastAPI, source_id: str) -> bool:
- """Whether this source's conversations may become memories."""
+ """Whether this source's conversations may become memories.
+
+ A hand-added memory is allowed unless its grant was explicitly revoked;
+ a missing grant row is allowed. The SDK add path writes no grant row, so
+ requiring one left every SDK-authority manual add unclaimable: Pending
+ forever with attempts=0 while later jobs ran. Intake, claim and execute
+ all ask this one question.
+ """
if source_id == "one-memory-manual":
receipt = _grant_store(app).receipt(user_id="local", source_id=source_id)
- return receipt is not None and receipt.revoked_at is None
+ return receipt is None or receipt.revoked_at is None
return _source_processing_enabled(app, source_id)
diff --git a/memhub/apps/core/src/one_memory/memory_additions.py b/memhub/apps/core/src/one_memory/memory_additions.py
index 3736c6c..89fa297 100644
--- a/memhub/apps/core/src/one_memory/memory_additions.py
+++ b/memhub/apps/core/src/one_memory/memory_additions.py
@@ -130,10 +130,18 @@ class MemoryAdditionStore:
raise RuntimeError("sdk_memory_add_intake_unavailable")
record = materialize_write_input(value, observed_at=now)
with main_database(self.write_queue.database_path).read() as database:
+ # No grant row is written here: a missing row means allowed,
+ # only an explicit revocation refuses (same as the claim gate).
+ grant = database.execute(
+ "SELECT revoked_at FROM source_grants WHERE user_id='local' AND source_id=?",
+ (SOURCE_ID,),
+ ).fetchone()
existing = database.execute(
"SELECT request_id FROM memory_note_requests WHERE note_id=?",
(record.evidence_id,),
).fetchone()
+ if grant is not None and grant[0] is not None:
+ raise ValueError("memory_add_source_revoked")
if existing is not None:
current = self.require(record.evidence_id)
if current.verbatim != record.verbatim:
diff --git a/memhub/apps/core/tests/test_memory_additions.py b/memhub/apps/core/tests/test_memory_additions.py
index e4952f9..b579cc4 100644
--- a/memhub/apps/core/tests/test_memory_additions.py
+++ b/memhub/apps/core/tests/test_memory_additions.py
@@ -166,6 +166,57 @@ def test_revoked_manual_source_is_not_silently_reauthorized(tmp_path):
asyncio.run(scenario())
+def test_sdk_manual_add_without_a_grant_row_is_claimed(tmp_path, monkeypatch):
+ """The SDK add path writes no grant row; the claim gate used to require one.
+
+ The intake admitted the job, the drain skipped it on every tick, and the
+ note stayed Pending with attempts=0 while later jobs ran. A missing grant
+ row is allowed; only an explicit revocation holds the job.
+ """
+
+ async def scenario():
+ app = application(tmp_path)
+ async with app.router.lifespan_context(app), client_for(app) as client:
+ addition = {"request_id": str(uuid4()), "text": "Remember this.", "scope": "all-apps"}
+ assert (await client.post("/api/v2/memories", json=addition)).status_code == 201
+ database = app.state.settings.data_dir / "one-memory.sqlite3"
+ with sqlite3.connect(database) as db:
+ assert (
+ db.execute(
+ "SELECT COUNT(*) FROM source_grants WHERE source_id='one-memory-manual'"
+ ).fetchone()[0]
+ == 0
+ )
+ request_id = db.execute("SELECT request_id FROM memory_note_requests").fetchone()[0]
+ job = app.state.memory_write_queue.get(request_id)
+ assert job is not None and job.state == "pending"
+ monkeypatch.setattr(
+ app.state.model_profile_store, "active_identity", lambda: ("profile", 1)
+ )
+
+ assert app.state.memory_write_drain.claim(limit=1) == (job.job_id,)
+
+ # An explicit revocation still holds the job and refuses new adds.
+ app.state.memory_write_queue.settle(job.job_id, state="pending", owner=None)
+ with sqlite3.connect(database) as db:
+ db.execute(
+ "INSERT INTO source_grants (user_id,source_id,root_path,requested_path,"
+ "root_identity,candidate_kind,granted_at,revoked_at,revision) VALUES "
+ "('local','one-memory-manual','x','x','manual','default',"
+ "'2026-10-01T00:00:00+00:00','2026-10-27T00:00:00+00:00',1)"
+ )
+ assert app.state.memory_write_drain.claim(limit=1) == ()
+ refused = await client.post(
+ "/api/v2/memories",
+ json={**addition, "request_id": str(uuid4())},
+ )
+ assert refused.status_code == 409
+ assert refused.json()["detail"] == "memory_add_source_revoked"
+
+ asyncio.run(scenario())
+
+
@pytest.mark.fresh_sdk
def test_sdk_memory_write_accepts_openai_messages_without_evidence_tables(tmp_path):
async def scenario():
diff --git a/memhub/apps/core/tests/test_policy_api_wiring.py b/memhub/apps/core/tests/test_policy_api_wiring.py
index af32e14..23c1e62 100644
--- a/memhub/apps/core/tests/test_policy_api_wiring.py
+++ b/memhub/apps/core/tests/test_policy_api_wiring.py
@@ -127,6 +127,28 @@ def test_the_manual_source_is_admitted_by_grant_without_a_toggle(tmp_path) -> No
assert _sedimentation_claim_enabled(app, "one-memory-manual") is False
+def test_the_manual_source_is_allowed_without_a_grant_row_until_revoked(tmp_path) -> None:
+ """The SDK add path writes no grant row, and intake admits the record.
+
+ Claim must answer the same: a missing row is allowed, an explicit
+ revocation is not.
+ """
+ receipt: list[object] = [None]
+ app = SimpleNamespace(
+ state=SimpleNamespace(
+ config_store=ConfigStore(tmp_path),
+ model_profile_store=SimpleNamespace(active_identity=lambda: ("profile", 1)),
+ source_grant_store=SimpleNamespace(receipt=lambda **_kwargs: receipt[0]),
+ )
+ )
+
+ assert _source_writes_on(app, "one-memory-manual") is True
+ assert _sedimentation_claim_enabled(app, "one-memory-manual") is True
+ receipt[0] = SimpleNamespace(revoked_at="2026-10-27")
+ assert _source_writes_on(app, "one-memory-manual") is False
+ assert _sedimentation_claim_enabled(app, "one-memory-manual") is False
+
+
def test_claim_gate_waits_without_polling_when_no_model_is_active(tmp_path) -> None:
config = ConfigStore(tmp_path)
app = SimpleNamespace(