Skip to content
Python

fix(memhub): set the SDK context recall lanes apart

alvinfei2fed7adf6b88
diff --git a/memhub/apps/core/src/one_memory/context_models.py b/memhub/apps/core/src/one_memory/context_models.py
index 907ba6c..6d02ee7 100644
--- a/memhub/apps/core/src/one_memory/context_models.py
+++ b/memhub/apps/core/src/one_memory/context_models.py
@@ -47,6 +47,19 @@ class ContextSearchRequest(BaseModel):
return self
+class SdkContextSearchRequest(ContextSearchRequest):
+ """`POST /api/v2/context/search`: the three SDK recall lanes are set apart.
+
+ `limit` is the normal lane's top-k and the cap on returned items;
+ `schema_limit` and `intention_limit` are the SDK's schema (L6) and
+ proactive intention (L7) lanes. A caller's values are passed through.
+ """
+
+ limit: int = Field(default=10, ge=1, le=20, strict=True)
+ schema_limit: int = Field(default=3, ge=0, le=20, strict=True)
+ intention_limit: int = Field(default=1, ge=0, le=20, strict=True)
+
+
class ContextMessage(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
diff --git a/memhub/apps/core/src/one_memory/context_routes.py b/memhub/apps/core/src/one_memory/context_routes.py
index c434939..43c400d 100644
--- a/memhub/apps/core/src/one_memory/context_routes.py
+++ b/memhub/apps/core/src/one_memory/context_routes.py
@@ -34,6 +34,7 @@ from one_memory.context_models import (
MemoryContextItem,
RawContextItem,
SdkContextItem,
+ SdkContextSearchRequest,
SdkContextSearchResponse,
SdkMemoryContextItem,
SdkRawContextItem,
@@ -439,7 +440,7 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
@router.post("/search", response_model=SdkContextSearchResponse)
async def search(
- value: ContextSearchRequest,
+ value: SdkContextSearchRequest,
request: Request,
response: Response,
) -> SdkContextSearchResponse:
@@ -514,8 +515,8 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
limit=value.limit,
min_score=sdk_relevance_floor(),
schema_min_score=sdk_relevance_floor(),
- schema_limit=value.limit,
- intention_limit=value.limit,
+ schema_limit=value.schema_limit,
+ intention_limit=value.intention_limit,
scene="normal",
reader=HY_MEMORY_READER,
)
diff --git a/memhub/apps/core/tests/test_memory_notes.py b/memhub/apps/core/tests/test_memory_notes.py
index 5a7cd30..4874f9e 100644
--- a/memhub/apps/core/tests/test_memory_notes.py
+++ b/memhub/apps/core/tests/test_memory_notes.py
@@ -440,3 +440,54 @@ def test_sdk_memory_library_uses_sdk_search_and_skips_empty_rows_without_mutatio
assert "source:default" not in node_ids
asyncio.run(scenario())
+
+
+def test_sdk_context_search_sets_each_recall_lane_apart(tmp_path):
+ """Normal, schema and intention lanes default to 10 / 3 / 1 and pass through."""
+
+ async def scenario():
+ app = application(tmp_path)
+ calls = []
+
+ class Client:
+ def search(self, query, **kwargs):
+ calls.append(kwargs)
+ return {"memories": {"schema": [], "proactive": [], "normal": []}}
+
+ def get(self, memory_id):
+ return None
+
+ sdk = Client()
+
+ class Handle:
+ async def client(self):
+ return sdk
+
+ async def close(self):
+ return None
+
+ async with app.router.lifespan_context(app):
+ app.state.sdk_client_handle = Handle()
+ async with client_for(app) as client:
+ defaulted = await client.post(
+ "/api/v2/context/search",
+ json={"query": "release", "kinds": ["memory"]},
+ )
+ chosen = await client.post(
+ "/api/v2/context/search",
+ json={
+ "query": "release",
+ "kinds": ["memory"],
+ "limit": 5,
+ "schema_limit": 0,
+ "intention_limit": 2,
+ },
+ )
+ assert defaulted.status_code == 200
+ assert chosen.status_code == 200
+ assert [
+ (call["limit"], call["schema_limit"], call["intention_limit"]) for call in calls
+ ] == [(10, 3, 1), (5, 0, 2)]
+
+ asyncio.run(scenario())
diff --git a/memhub/packages/host-bridge/src/context-integration.test.ts b/memhub/packages/host-bridge/src/context-integration.test.ts
index 1eed976..63a200e 100644
--- a/memhub/packages/host-bridge/src/context-integration.test.ts
+++ b/memhub/packages/host-bridge/src/context-integration.test.ts
@@ -317,6 +317,39 @@ it("selects SDK Context v2 after cutover and passes native fields unchanged", as
);
});
+it.each([
+ [10, 200],
+ [11, 502],
+])(
+ "accepts up to Core's v2 default of 10 items when no limit is sent (%i items)",
+ async (count, status) => {
+ const f = await fixture();
+ const items = Array.from({ length: count }, (_, index) => ({
+ kind: "memory",
+ memory_id: `memory-${index}`,
+ content: "m",
+ layer: "l2_fact",
+ status: "active",
+ source: "codex",
+ updated_at: "2026-10-21T00:00:00Z",
+ score: 0.5,
+ truncated: false,
+ }));
+ f.setAuthority("sdk");
+ f.respond({
+ schema_version: 2,
+ request_id: "sdk-request",
+ state: "grounded",
+ items,
+ used_characters: count,
+ truncated: false,
+ total_ms: 1,
+ });
+
+ expect((await f.post({ query: "original" })).status).toBe(status);
+ },
+);
+
it("does not fall back to legacy context when the selected SDK response is invalid", async () => {
const f = await fixture();
f.setAuthority("sdk");
diff --git a/memhub/packages/host-bridge/src/context-protocol.ts b/memhub/packages/host-bridge/src/context-protocol.ts
index 1ad733d..1f455e8 100644
--- a/memhub/packages/host-bridge/src/context-protocol.ts
+++ b/memhub/packages/host-bridge/src/context-protocol.ts
@@ -74,7 +74,7 @@ export const CONTEXT_INPUT_SCHEMA = {
description:
"Filters raw only, within already granted sources; does not change memory scope.",
},
- limit: { type: "integer", minimum: 1, maximum: 20, default: 8 },
+ limit: { type: "integer", minimum: 1, maximum: 20, default: 10 },
character_budget: {
type: "integer",
minimum: 200,
@@ -285,8 +285,10 @@ export function isContextResponseForRequest(
): value is HostContextSearchResponse {
if (!isHostContextSearchResponse(value)) return false;
const kinds = request.kinds ?? ["raw", "memory"];
+ // Core's default item cap: 8 on v1, 10 on SDK-native v2.
+ const limit = request.limit ?? (isSdkContextSearchResponse(value) ? 10 : 8);
return (
- value.items.length <= (request.limit ?? 8) &&
+ value.items.length <= limit &&
value.used_characters <= (request.character_budget ?? 4000) &&
value.items.every(
(item) =>