Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(memory): fail closed on managed cache recovery

cyh81cb73a29cc1
diff --git a/config/v2_config.py b/config/v2_config.py
index 64b710480..48aafadd7 100644
--- a/config/v2_config.py
+++ b/config/v2_config.py
@@ -1120,6 +1120,15 @@ def _recovery_field_for_error(section: Optional[str], error: BaseException) -> O
de-duplicating — as a whole, which is what stops the recovery loop.
"""
+ if section == "memory.cloud":
+ match = re.search(r"Config '([^']+)'", str(error))
+ if not match:
+ return None
+ path = match.group(1)
+ prefix = "memory.cloud."
+ if not path.startswith(prefix):
+ return None
+ return path[len(prefix) :].split(".", 1)[0]
if section not in _FIELD_SCOPED_RECOVERY_SECTIONS:
return None
match = re.search(r"Config '([^']+)'", str(error))
@@ -1183,6 +1192,87 @@ def _recover_switch_section_field(
return False
+def _memory_cloud_recovery_requires_managed_fence(payload: dict) -> bool:
+ """Fail closed when a paired instance is not authoritatively personal."""
+
+ remote_access = payload.get("remote_access")
+ if not isinstance(remote_access, dict):
+ return False
+ vibe_cloud = remote_access.get("vibe_cloud")
+ if not isinstance(vibe_cloud, dict):
+ return False
+ instance_kind = vibe_cloud.get("instance_kind")
+ if instance_kind == "personal":
+ return False
+ if instance_kind == "organization":
+ return True
+ return bool(
+ vibe_cloud.get("enabled") is True
+ and isinstance(vibe_cloud.get("instance_id"), str)
+ and vibe_cloud.get("instance_id", "").strip()
+ and isinstance(vibe_cloud.get("instance_secret"), str)
+ and vibe_cloud.get("instance_secret", "").strip()
+ )
+
+
+def _arm_memory_cloud_recovery(memory: dict) -> None:
+ """Keep an unknown cloud identity behind the existing rebuild ladder."""
+
+ if memory.get("recovery_intent") is None:
+ memory["recovery_intent"] = "rebuild"
+
+
+def _recover_memory_cloud_section(payload: dict, field_name: Optional[str]) -> bool:
+ """Recover one cloud-cache member without losing valid runtime ownership."""
+
+ memory = payload.get("memory")
+ if not isinstance(memory, dict):
+ return False
+ cloud = memory.get("cloud")
+ managed_fence = _memory_cloud_recovery_requires_managed_fence(payload)
+ if not isinstance(cloud, dict) or field_name is None:
+ if managed_fence:
+ memory["cloud"] = {
+ "scope": "organization",
+ "organization_attached": True,
+ "runtime_apply_pending": True,
+ }
+ _arm_memory_cloud_recovery(memory)
+ else:
+ memory["cloud"] = {}
+ if memory.get("mode") == "platform":
+ _arm_memory_cloud_recovery(memory)
+ return True
+
+ cloud.pop(field_name, None)
+ if field_name == "runtime_apply_pending":
+ cloud["runtime_apply_pending"] = True
+ elif field_name == "applied_embedding_identity":
+ live_identity = cloud.get("embedding_identity")
+ if isinstance(live_identity, str) and live_identity.strip():
+ cloud["applied_embedding_identity"] = live_identity
+ else:
+ _arm_memory_cloud_recovery(memory)
+ elif field_name == "source_instance_id":
+ cloud["capabilities"] = {}
+ cloud["embedding_identity"] = None
+ cloud["model_access_key"] = None
+ cloud["proxy_base_url"] = None
+ _arm_memory_cloud_recovery(memory)
+
+ if managed_fence and field_name in {
+ "scope",
+ "organization_attached",
+ "transition_notice_pending",
+ }:
+ cloud["scope"] = "organization"
+ cloud["organization_attached"] = True
+ cloud["transition_notice_pending"] = False
+ cloud["transition_rebuild_owned"] = False
+ _arm_memory_cloud_recovery(memory)
+ return True
+
+
def _reset_recoverable_config_section(
payload: dict, section: str, field_name: Optional[str] = None
) -> bool:
@@ -1210,11 +1300,7 @@ def _reset_recoverable_config_section(
processing.pop("multimodal", None)
return True
if section == "memory.cloud":
- memory = payload.get("memory")
- if not isinstance(memory, dict):
- return False
- memory["cloud"] = {}
- return True
+ return _recover_memory_cloud_section(payload, field_name)
if section == "runtime":
# Keep this in sync with ``V2Config.default``. RuntimeConfig has a
# required cwd, so an empty object would make the recovery loop fail a
diff --git a/tests/test_memory_config.py b/tests/test_memory_config.py
index 4f13d653d..7683ea453 100644
--- a/tests/test_memory_config.py
+++ b/tests/test_memory_config.py
@@ -395,7 +395,14 @@ def test_disk_load_recovers_only_a_malformed_memory_cloud_cache(
assert loaded.memory.custom_processing_complete() is True
assert loaded.memory.processing.llm.api_key == "llm-key"
assert loaded.memory.processing.embedding.api_key == "embed-key"
- assert loaded.memory.recovery_intent == recovery_intent
+ assert loaded.memory.recovery_intent == (
+ recovery_intent
+ or (
+ "rebuild"
+ if mode == "platform" and not isinstance(cloud, dict)
+ else None
+ )
+ )
assert loaded.memory.cloud == MemoryCloudConfig()
assert loaded.memory.runtime_source() == runtime_source
assert loaded.memory.cloud_runtime_selected() is (mode == "platform")
@@ -404,6 +411,186 @@ def test_disk_load_recovers_only_a_malformed_memory_cloud_cache(
assert json.loads(config_path.read_text(encoding="utf-8"))["memory"]["cloud"] == cloud
+def _acknowledged_organization_cloud() -> dict:
+ return {
+ "scope": "organization",
+ "capabilities": {
+ "asr": False,
+ "chat": True,
+ "embedding": True,
+ "multimodal": False,
+ },
+ "embedding_identity": "emb-org",
+ "revision": 4,
+ "quota_enforced": False,
+ "model_access_key": "mak_org",
+ "proxy_base_url": "https://backend.example.test/v1/model",
+ "source_instance_id": "instance-org",
+ "organization_attached": True,
+ "transition_notice_pending": False,
+ "transition_rebuild_owned": False,
+ "applied_embedding_identity": "emb-org",
+ "runtime_apply_pending": False,
+ }
+
+
+ ("field", "invalid"),
+ [
+ ("scope", "unsupported"),
+ ("capabilities", []),
+ ("embedding_identity", 7),
+ ("revision", "four"),
+ ("quota_enforced", []),
+ ("model_access_key", 7),
+ ("proxy_base_url", 7),
+ ("source_instance_id", 7),
+ ("organization_attached", []),
+ ("transition_notice_pending", []),
+ ("transition_rebuild_owned", []),
+ ("applied_embedding_identity", 7),
+ ("runtime_apply_pending", []),
+ ],
+)
+def test_disk_cloud_field_recovery_never_reactivates_custom_for_an_acknowledged_org(
+ tmp_path,
+ field: str,
+ invalid: object,
+) -> None:
+ config_path = tmp_path / "config.json"
+ cloud = _acknowledged_organization_cloud()
+ cloud[field] = invalid
+ payload = _payload(
+ {
+ "enabled": True,
+ "mode": "custom",
+ "processing": _complete_processing(),
+ "cloud": cloud,
+ }
+ )
+ payload["remote_access"] = {
+ "vibe_cloud": {
+ "enabled": True,
+ "instance_id": "instance-org",
+ "instance_secret": "device-secret",
+ "instance_kind": "organization",
+ }
+ }
+ config_path.write_text(json.dumps(payload), encoding="utf-8")
+
+ loaded = V2Config.load(config_path)
+
+ assert loaded.memory.custom_processing_complete() is True
+ assert loaded.memory.processing.llm.api_key == "llm-key"
+ assert loaded.memory.processing.embedding.api_key == "embed-key"
+ assert loaded.memory.settings_mode() == "organization"
+ assert loaded.memory.cloud_runtime_selected() is True
+ assert loaded.memory.runtime_source() != "custom"
+ assert any("memory.cloud" in warning for warning in loaded.load_warnings)
+
+
[email protected]("instance_kind", ["organization", ""])
+def test_disk_whole_cloud_recovery_fails_closed_for_managed_or_unknown_pairing(
+ tmp_path,
+ instance_kind: str,
+) -> None:
+ config_path = tmp_path / "config.json"
+ payload = _payload(
+ {
+ "enabled": False,
+ "mode": "custom",
+ "processing": _complete_processing(),
+ "cloud": [],
+ }
+ )
+ payload["remote_access"] = {
+ "vibe_cloud": {
+ "enabled": True,
+ "instance_id": "instance-org",
+ "instance_secret": "device-secret",
+ "instance_kind": instance_kind,
+ }
+ }
+ config_path.write_text(json.dumps(payload), encoding="utf-8")
+
+ loaded = V2Config.load(config_path)
+
+ assert loaded.memory.settings_mode() == "organization"
+ assert loaded.memory.runtime_source() == "unavailable"
+ assert loaded.memory.recovery_intent == "rebuild"
+ assert loaded.memory.enabled is False
+ assert loaded.memory.custom_processing_complete() is True
+
+
+def test_disk_whole_cloud_recovery_keeps_personal_custom_runtime(
+ tmp_path,
+) -> None:
+ config_path = tmp_path / "config.json"
+ payload = _payload(
+ {
+ "enabled": True,
+ "mode": "custom",
+ "processing": _complete_processing(),
+ "cloud": [],
+ }
+ )
+ payload["remote_access"] = {
+ "vibe_cloud": {
+ "enabled": True,
+ "instance_id": "instance-personal",
+ "instance_secret": "device-secret",
+ "instance_kind": "personal",
+ }
+ }
+ config_path.write_text(json.dumps(payload), encoding="utf-8")
+
+ loaded = V2Config.load(config_path)
+
+ assert loaded.memory.settings_mode() == "custom"
+ assert loaded.memory.runtime_source() == "custom"
+ assert loaded.memory.recovery_intent is None
+
+
+def test_disk_cloud_field_recovery_preserves_proven_no_pair_org_grandfathering(
+ tmp_path,
+) -> None:
+ config_path = tmp_path / "config.json"
+ payload = _payload(
+ {
+ "enabled": True,
+ "mode": "custom",
+ "processing": _complete_processing(),
+ "cloud": {
+ "scope": "organization",
+ "capabilities": {
+ "asr": False,
+ "chat": False,
+ "embedding": False,
+ "multimodal": False,
+ },
+ "revision": "unreadable",
+ "organization_attached": False,
+ },
+ }
+ )
+ payload["remote_access"] = {
+ "vibe_cloud": {
+ "enabled": True,
+ "instance_id": "instance-org",
+ "instance_secret": "device-secret",
+ "instance_kind": "organization",
+ }
+ }
+ config_path.write_text(json.dumps(payload), encoding="utf-8")
+
+ loaded = V2Config.load(config_path)
+
+ assert loaded.memory.settings_mode() == "custom"
+ assert loaded.memory.runtime_source() == "custom"
+ assert loaded.memory.recovery_intent is None
+ assert loaded.memory.custom_processing_complete() is True
+
+
def test_memory_transition_rebuild_owner_round_trips(tmp_path) -> None:
config_path = tmp_path / "config.json"
config = V2Config.from_payload(
diff --git a/tests/test_model_service.py b/tests/test_model_service.py
index 7f98e542d..279bf1515 100644
--- a/tests/test_model_service.py
+++ b/tests/test_model_service.py
@@ -715,6 +715,31 @@ def test_fresh_managed_instance_enables_when_organization_adds_memory_pair() ->
assert activated.recovery_intent is None
+def test_recovered_cloud_rebuild_fence_prevents_fresh_activation_override() -> None:
+ recovered = MemoryConfig(
+ enabled=False,
+ mode="custom",
+ processing=_manual_memory().processing,
+ recovery_intent="rebuild",
+ cloud=MemoryCloudConfig(
+ scope="organization",
+ source_instance_id="instance-1",
+ organization_attached=True,
+ ),
+ )
+
+ activated = _resolved(
+ recovered,
+ _status(scope="organization", identity="emb-org", revision=2),
+ minted=_mint(),
+ )
+
+ assert activated.enabled is False
+ assert activated.cloud.applied_embedding_identity == "emb-org"
+ assert activated.runtime_source() == "cloud"
+ assert activated.recovery_intent == "rebuild"
+
+
def test_fresh_platform_instance_enables_when_memory_capabilities_recover() -> None:
waiting = _resolved(
MemoryConfig(),
diff --git a/vibe/model_service.py b/vibe/model_service.py
index 71de9fb84..2f300229b 100644
--- a/vibe/model_service.py
+++ b/vibe/model_service.py
@@ -201,7 +201,7 @@ def _adopt_cloud_embedding_identity(
"""Record one cloud target while checking the prior runtime baseline."""
baseline = previous.cloud.applied_embedding_identity
- first_activation = baseline is None
+ first_activation = baseline is None and previous.recovery_intent is None
if previous.cloud_runtime_selected() and baseline is not None and baseline != identity:
candidate.arm_rebuild_if_idle()
candidate.cloud.applied_embedding_identity = identity