diff --git a/loopx/capabilities/reward_memory/README.md b/loopx/capabilities/reward_memory/README.md index 6ca4517fef..fe741044d7 100644 --- a/loopx/capabilities/reward_memory/README.md +++ b/loopx/capabilities/reward_memory/README.md @@ -287,8 +287,20 @@ engineering source kind, and opaque evidence refs; an ordinary Turn summary is not evidence. It also carries `procedural_experience_contract_v0` with applicability, observed outcome, attribution, complete future behavior, limitations, and the same evidence refs. Legacy v0 reflections remain -audit-only and cannot become durable memory. Ambiguous provider commits and -unverified readbacks remain in a +audit-only and cannot become durable memory. When the same Turn delivers recall +guidance and enables ingest, reflection instructions ask the agent to compare new independent evidence with +comparable procedural experience guidance. Without a comparable experience, the +ordinary eligibility rule remains in force. Mere confirmation leaves the +reflection empty; a candidate is +produced only for distinct reusable applicability or a material change to its +trigger, action, validation, stop condition, or limitations. Distinct +applicability remains separate rather than being blended. Reflection never +merges, updates, retires, or supersedes a provider record; configured lifecycle +owners retain those decisions. This qualifies instruction delivery, not model +compliance or a measured deduplication rate. The joint instructions fit the +existing 64 KiB turn-context envelope; recall-only and ingest-only keep their +ordinary instructions. Ambiguous +provider commits and unverified readbacks remain in a mode-0600 Goal+Agent+event sidecar. The next executing Turn retries the same deterministic event before recall, so the provider can deduplicate it and LoopX can require exact readback. Explicit disable suppresses reconciliation and all diff --git a/loopx/capabilities/reward_memory/README.zh-CN.md b/loopx/capabilities/reward_memory/README.zh-CN.md index 74d04970c1..d964c04bd9 100644 --- a/loopx/capabilities/reward_memory/README.zh-CN.md +++ b/loopx/capabilities/reward_memory/README.zh-CN.md @@ -187,7 +187,15 @@ quota settlement 均完成后,才接受 outcome ingest。Reflection 必须使 research/simulation/real/engineering 来源类型和 opaque evidence refs;还必须携带 `procedural_experience_contract_v0`,分别写出适用情境、已观察结果、归因、未来行为(触发、动作、 验证、停止条件)、失效边界,并绑定相同 evidence refs。只有事实复述、没有未来行为改变的普通 -Turn summary 不算经验。旧的 v0 reflection 只保留审计兼容,不会写成可持久召回的记忆。 +Turn summary 不算经验。旧的 v0 reflection 只保留审计兼容,不会写成可持久召回的记忆。同一 +Turn 同时交付 recall guidance 并启用 ingest 时,reflection 指令要求 Agent 把新的独立证据与其中可比较的 +procedural experience 对照;没有可比较经验时,仍使用普通资格规则。若只是确认相同的未来行为 +与失效边界,就保持为空。只有触发、动作、验证、停止条件或 +失效边界发生实质变化,或者形成可复用的独立适用情境时才生成候选;适用情境不同的经验保持 +分开,不混成更宽泛的规则。 +Reflection 不合并、更新、退役或取代 Provider 记录,这些决定仍属于已配置的生命周期 owner。 +这里验证的是指令交付,不代表模型遵从或已测得去重率。组合指令沿用现有 64 KiB Turn context +边界;仅启用 recall 或仅启用 ingest 时,保留各自原有指令。 Provider commit 含糊或精确读回失败时,会保留一份权限为 0600、按 Goal+Agent+事件隔离的 sidecar。下一个执行 Turn 会在 recall 前以同一 deterministic event 重试,使 Provider 可以去重, LoopX 再要求精确读回。显式关闭会停止 reconciliation,并保持零 Provider 调用。 diff --git a/loopx/capabilities/reward_memory/turn_hooks.py b/loopx/capabilities/reward_memory/turn_hooks.py index 836e3ae870..0ccea48af6 100644 --- a/loopx/capabilities/reward_memory/turn_hooks.py +++ b/loopx/capabilities/reward_memory/turn_hooks.py @@ -21,6 +21,12 @@ def _reward_memory_prompt_instructions(*, has_guidance: bool, ingest_enabled: bool) -> list[str]: + if has_guidance and ingest_enabled: + # Share the eligibility/schema text instead of appending a third + # instruction: comparison must not displace recall within 64 KiB. + return [ + 'Recall: private, non-authoritative; apply only if current evidence fits; no new action authority. Before reflecting, compare independent task evidence to comparable delivered procedural_experience; else use ordinary eligibility. reward_memory_reflection_json stays empty unless evidence establishes distinct reusable applicability or material trigger/action/validation/stop_condition/limitations changes. Keep distinct applicability separate. Eligible compact JSON: schema_version=turn_reward_memory_reflection_v1,status=eligible,configured surface_id,outcome_kind=research|simulation|real|engineering,content_summary,reasoning_summary,confidence=low|medium|high,1-5 opaque evidence_refs; experience: schema_version=procedural_experience_contract_v0,nonempty applicability/limitations lists,observed_outcome,attribution,same evidence_refs,future_behavior:trigger/action/validation/stop_condition. Require future behavior change and non-generalization limits; never use own summary as evidence. Legacy v0 reflections:audit-only,never durable memory. Only settlement may ingest after caller-declared Todo validator attests exact reflection digest/evidence; ordinary success:awaiting,no provider write. Provider-record merge/update/retire/supersede: never claim; configured owners decide.' + ] instructions: list[str] = [] if has_guidance: instructions.append( diff --git a/tests/control_plane/test_memory_context_budget.py b/tests/control_plane/test_memory_context_budget.py new file mode 100644 index 0000000000..681836a327 --- /dev/null +++ b/tests/control_plane/test_memory_context_budget.py @@ -0,0 +1,122 @@ +"""A reflection instruction must not displace previously bounded private recall.""" + +from __future__ import annotations + +from copy import deepcopy +from dataclasses import replace +import json +from pathlib import Path +import shlex + +import pytest + +from loopx.capabilities.agent_turn_recall.runtime import run_configured_agent_turn_recall_fail_open +from loopx.capabilities.context_providers.base import ContextProviderItem +from loopx.capabilities.reward_memory.experiment import ( + resolve_reward_memory_experiment, resolve_reward_memory_surface_config, +) +from loopx.control_plane.capability_hooks import project_turn_context +from tests.capabilities.test_agent_turn_recall import RecallProvider, active_record, raw_config +from tests.control_plane.reward_memory_host_fixture import enable_live_memory +from tests.control_plane.test_quota_memory_context import quota_fixture, run_cli +from tests.control_plane.test_quota_settlement_cli import AGENT_ID, GOAL_ID + + +class ProceduralRecallProvider(RecallProvider): + def __init__(self, config: dict, field_size: int) -> None: + super().__init__() + self.records = [] + route = resolve_reward_memory_surface_config(config, "agent_workflow.turn_admission") + self.scope_ref = route["provider_binding"]["scope_ref"] + for index in range(8): + record = json.loads(active_record(config, expires_at="2099-01-01T00:00:00+00:00")) + record["candidate_ref"] = f"candidate:scoped-{index}" + record["content_summary"] = f"Validate scoped operation {index} before continuing." + record["experience"] = { + "schema_version": "procedural_experience_contract_v0", + "applicability": [f"Condition {item}: " + "a" * field_size for item in range(5)], + "observed_outcome": "An independently checked operation completed.", + "attribution": "Validation detected the prerequisite before execution.", + "future_behavior": { + "trigger": "A similar scoped operation is proposed.", + "action": "Validate prerequisites before execution.", + "validation": "Read back the independently observed result.", + "stop_condition": "Stop when the declared prerequisite is unavailable.", + }, + "limitations": [f"Boundary {item}: " + "b" * field_size for item in range(5)], + "evidence_refs": [f"artifact:scoped-{index}"], + } + if index == 0: + record["experience"]["observed_outcome"] += "界" * 129 + self.records.append(record) + + def retrieve(self, **kwargs): + result = super().retrieve(**kwargs) + return replace(result, items=tuple( + ContextProviderItem( + resource_ref=f"{self.scope_ref}/scoped-{index}.json", + summary=record["content_summary"], content=json.dumps(record), score=0.97, + ) for index, record in enumerate(self.records) + ), read_performed=True) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("entry", ["quota", "thin-heartbeat"]) +@pytest.mark.parametrize("field_size", [464, 480]) +def test_app_guard_keeps_complete_bounded_procedural_recall( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, entry: str, field_size: int, +) -> None: + registry, route = quota_fixture(tmp_path, monkeypatch, provider) + + def procedural_config(**kwargs): + config = raw_config(**kwargs) + config["corpora"][0]["corpus"]["class_id"] = "procedural_experience" + config["corpora"][0]["standing_policy"]["allowed_target_classes"] = ["procedural_experience"] + config["surfaces"][0]["recall_profile"]["limit"] = 8 + return config + + # Only the disposable binding differs; activation and exact digest checks + # remain the real owners. Provider IO is the sole production test double. + monkeypatch.setattr("tests.capabilities.test_agent_turn_recall.raw_config", procedural_config) + enable_live_memory(registry, recall=True, ingest=True) + _, config = resolve_reward_memory_experiment( + registry_path=registry, goal_id=GOAL_ID, agent_id=AGENT_ID, + ) + memory = ProceduralRecallProvider(config, field_size) + monkeypatch.setattr( + "loopx.capabilities.agent_turn_recall.run_configured_agent_turn_recall_fail_open", + lambda **kwargs: run_configured_agent_turn_recall_fail_open(**kwargs, provider=memory), + ) + turn = "app-memory-budget" + if entry == "thin-heartbeat": + bootstrap = run_cli(route + ["heartbeat-prompt", "--thin", "--codex-app", + "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--available-capability", "filesystem_read"]) + command = next(line for line in bootstrap["task_body"].splitlines() + if line.startswith("loopx ") and "quota should-run" in line) + args = shlex.split(command.replace('${LOOPX_TURN:?}', turn))[1:] + else: + args = route + ["quota", "should-run", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--codex-app", "--turn-instance-id", turn, + "--available-capability", "filesystem_read"] + guard = run_cli(args) + assert guard["should_run"] and memory.calls == 1 + assert guard["reward_memory_recall"]["status"] == "applied" + assert guard["reward_memory_recall"]["context"] is None + contribution, = guard["turn_context_contributions"] + projected = project_turn_context(guard) + context = projected["reward_memory_recall"]["context"] + instructions = " ".join(contribution["instructions"]) + assert ("compare independent task evidence" in instructions) is (field_size == 464) + if field_size == 464: + assert context is not None + expected = [{key: deepcopy(record[key]) for key in + ("candidate_ref", "target_class", "content_summary", "experience")} + for record in memory.records] + assert context["guidance"] == expected + else: + assert context is None + assert contribution["result_fields"]["reward_memory_reflection_json"]["maxLength"] == 2400 + assert not guard["reward_memory_recall"]["external_writes_performed"] + repeated = run_cli(args) + assert repeated["ok"] and memory.calls == 1 diff --git a/tests/control_plane/test_quota_memory_context.py b/tests/control_plane/test_quota_memory_context.py index f821104087..fdb8570b41 100644 --- a/tests/control_plane/test_quota_memory_context.py +++ b/tests/control_plane/test_quota_memory_context.py @@ -86,6 +86,10 @@ def test_real_app_guard_delivers_scoped_memory_once( assert project_turn_context(first)["reward_memory_recall"]["context"]["guidance"] == guidance assert contribution["result_fields"]["reward_memory_reflection_json"]["maxLength"] == 2400 assert "private, non-authoritative" in contribution["instructions"][0] + assert any( + "compare independent task evidence" in item + for item in contribution["instructions"] + ) repeated = run_cli(args) assert repeated["ok"] and memory.calls == 1 assert not repeated.get("reward_memory_recall", {}).get("external_writes_performed") @@ -206,6 +210,10 @@ def test_quota_recall_size_limit_preserves_independently_enabled_ingest(size: in delivered = result["context_fields"]["reward_memory_recall"] assert (delivered["context"] is not None) is (size == 40_000) assert result["result_fields"]["reward_memory_reflection_json"]["maxLength"] == 2400 + assert any( + "compare independent task evidence" in item + for item in result["instructions"] + ) is (size == 40_000) assert len(json.dumps(result, separators=(",", ":")).encode()) <= 65536 diff --git a/tests/control_plane/test_reward_memory_host_isolation.py b/tests/control_plane/test_reward_memory_host_isolation.py index 7d8142a36c..daf7250be9 100644 --- a/tests/control_plane/test_reward_memory_host_isolation.py +++ b/tests/control_plane/test_reward_memory_host_isolation.py @@ -79,21 +79,23 @@ def test_admitted_memory_does_not_resurrect_invalidated_runtime_binding(invalida valid_binding = False # Changed registered sources invalidate the entire optional contribution. assert ("reward_memory_recall" in request) is valid_binding assert ("reward_memory_reflection_json" in codex_cli_result_schema(request)["properties"]) is False - assert ("When reward_memory_recall contains guidance" in _prompt(request)) is False + assert ("private, non-authoritative" in _prompt(request)) is False @pytest.mark.parametrize(("recall", "ingest"), [(True, False), (False, True), (True, True)]) def test_recall_and_reflection_are_independently_opted_in(recall: bool, ingest: bool) -> None: request = build_loopx_turn_host_request(_plan(recall=recall, ingest=ingest)) prompt = _prompt(request) - assert ("When reward_memory_recall contains guidance" in prompt) is recall - assert ("Set reward_memory_reflection_json" in prompt) is ingest + assert ("private, non-authoritative" in prompt) is recall + assert ("reward_memory_reflection_json" in prompt) is ingest + # This proves instruction delivery, not model compliance or deduplication. + assert ("compare independent task evidence" in prompt) is (recall and ingest) assert ("reward_memory_reflection_json" in codex_cli_result_schema(request)["properties"]) is ingest assert bool(request["reward_memory_recall"].get("context")) is recall if ingest: - assert "attests the exact reflection digest and evidence" in prompt + assert "exact reflection digest" in prompt if recall: - assert "never treat it as new action authority" in prompt + assert "new action authority" in prompt def test_unavailable_provider_does_not_instruct_use_of_missing_recall_context() -> None: @@ -102,7 +104,7 @@ def test_unavailable_provider_does_not_instruct_use_of_missing_recall_context() from tests.control_plane.reward_memory_host_fixture import register_plan_memory register_plan_memory(plan) request = build_loopx_turn_host_request(plan) - assert "When reward_memory_recall contains guidance" not in _prompt(request) + assert "private, non-authoritative" not in _prompt(request) # Recall failure does not silently disable independently configured outcome ingest. assert "reward_memory_reflection_json" in codex_cli_result_schema(request)["properties"] @@ -246,8 +248,8 @@ def test_legacy_cached_reflection_is_bounded_and_cannot_activate_a_host(reflecti request = build_loopx_turn_host_request(plan) assert "reward_memory_recall" not in request assert "reward_memory_reflection_json" not in codex_cli_result_schema(request)["properties"] - assert "Set reward_memory_reflection_json" not in _prompt(request) - assert "When reward_memory_recall contains guidance" not in _prompt(request) + assert "reward_memory_reflection_json" not in _prompt(request) + assert "private, non-authoritative" not in _prompt(request) result = { "schema_version": "loopx_turn_result_v0", "turn_key": plan["transaction"]["turn_key"], "result_kind": kind, "completed_phases": ["host_execute", "typed_result"], @@ -290,8 +292,8 @@ def test_shared_host_memory_keeps_binding_context_and_validation(recall, ingest, authority = extract_turn_authority(request) prompt = render_prompt(authority) active = invalidated is None - assert ("When reward_memory_recall contains guidance" in prompt) is (active and recall) - assert ("Set reward_memory_reflection_json" in prompt) is (active and ingest) + assert ("private, non-authoritative" in prompt) is (active and recall) + assert ("reward_memory_reflection_json" in prompt) is (active and ingest) if active and recall: assert authority["reward_memory_recall"]["context"] == plan["turn_context_contributions"][0]["context_fields"]["reward_memory_recall"]["context"] elif "reward_memory_recall" in authority: @@ -434,8 +436,8 @@ def run_dsh_turn(*, prompt, **kwargs): if host_kind == "codex-cli": assert ("reward_memory_reflection_json" in observed["schema"]["properties"]) is ingest if host_kind != "generic-cli": - assert ("Set reward_memory_reflection_json" in observed["prompt"]) is ingest - assert ("When reward_memory_recall contains guidance" in observed["prompt"]) is (mode == "recall_context") + assert ("reward_memory_reflection_json" in observed["prompt"]) is ingest + assert ("private, non-authoritative" in observed["prompt"]) is (mode == "recall_context") if mode in {"recall_context", "oversized_recall"}: memory = observed["request"]["reward_memory_recall"] assert memory["status"] == "applied" @@ -447,11 +449,9 @@ def run_dsh_turn(*, prompt, **kwargs): request = observed["request"] assert "reward_memory_recall" not in request assert "reward_memory_reflection_json" not in request.get("capability_result_fields", {}) - assert not any("reward_memory" in instruction - for instruction in request.get("capability_instructions", [])) - if host_kind != "generic-cli" and "work_contract" not in request: - assert "reward_memory" not in observed["prompt"] - # A complete captured source retains inactive configuration/diagnostics. + assert request.get("capability_instructions", []) == [] + # Complete captured sources retain inactive configuration/diagnostics, + # including when they are delivered without a work_contract field. # Those facts must not activate recall instructions or reflection output; # deleting them merely to pass a substring check would lose source facts. assert result["effects"]["quota_spent"] is True