From f58ba1415ed2bd62d8eb21c55afebc4e472828df Mon Sep 17 00:00:00 2001 From: Sylan1024 <255516746+ningsiyuan1024@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:35:16 +0800 Subject: [PATCH 1/2] test(sleep): require deduplicated context to survive task-file reload --- tests/test_preference_scope_contracts.py | 25 ++++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/test_preference_scope_contracts.py b/tests/test_preference_scope_contracts.py index c42f5f339..0415f844c 100644 --- a/tests/test_preference_scope_contracts.py +++ b/tests/test_preference_scope_contracts.py @@ -1,17 +1,11 @@ """Synthetic evidence contracts for #154; no real preference inference is tested.""" -import json - import pytest from skillopt_sleep.llm_miner import _digest_to_prompt from skillopt_sleep.mine import heuristic_mine, mine +from skillopt_sleep.tasks_file import load_tasks_file, make_tasks_payload, write_tasks_file from skillopt_sleep.types import SessionDigest - -class ScopeEvidenceLoss(AssertionError): - """Only this known retention gap is allowed to xfail.""" - - CASES = [ ("durable", "For all future status reports, use bullet points.", "Keep that preference for future reports.", "eligible_for_review"), @@ -50,13 +44,18 @@ def test_scope_evidence_survives_individual_heuristic_mining(case): @pytest.mark.parametrize("case", CASES, ids=[c[0] for c in CASES]) -@pytest.mark.xfail(strict=True, raises=ScopeEvidenceLoss, reason="Proposed #154 contract: dedup currently retains first context only") -def test_paired_scope_evidence_survives_public_mine_and_dedup(case): +def test_paired_scope_evidence_survives_public_mine_and_dedup(case, tmp_path): digests = sessions(case) records = mine(digests, val_fraction=0, test_fraction=0) assert len(records) == 1 assert records[0].source_sessions == [d.session_id for d in digests] - payload = json.dumps(records[0].to_dict()) - missing = [d.session_id for d in digests if d.user_prompts[-1] not in payload] - if missing: - raise ScopeEvidenceLoss(f"Missing scope evidence from sessions: {missing}") + + path = write_tasks_file(str(tmp_path / "tasks.json"), make_tasks_payload(records, project=digests[0].project)) + reloaded, _ = load_tasks_file(path) + record = reloaded[0] + context_by_session = record.to_dict().get("context_by_session", {}) + assert list(context_by_session) == [d.session_id for d in digests] + for digest in digests: + assert digest.user_prompts[-1] in context_by_session[digest.session_id] + assert digests[0].user_prompts[-1] in record.context_excerpt + assert digests[1].user_prompts[-1] not in record.context_excerpt From 83b5733eecdadc71fd33d2f0cdd39885b88ff24e Mon Sep 17 00:00:00 2001 From: Sylan1024 <255516746+ningsiyuan1024@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:36:35 +0800 Subject: [PATCH 2/2] fix(sleep): preserve follow-up context across task deduplication --- skillopt_sleep/mine.py | 5 ++++- skillopt_sleep/types.py | 4 +++- tests/PREFERENCE_SCOPE_MATRIX_154.md | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/skillopt_sleep/mine.py b/skillopt_sleep/mine.py index 069d35f4f..3edfa57f6 100644 --- a/skillopt_sleep/mine.py +++ b/skillopt_sleep/mine.py @@ -194,6 +194,7 @@ def heuristic_mine( context = "Follow-up constraints from the same session:\n- " + "\n- ".join( _short(p, 200) for p in d.user_prompts[1:4] ) + context = _short(context, 600) tags = [] if d.tools_used: tags.append("tools:" + "+".join(d.tools_used[:4])) @@ -205,7 +206,7 @@ def heuristic_mine( id=_tid(d.project, intent), project=d.project, intent=_short(intent, 800), - context_excerpt=_short(context, 600), + context_excerpt=context, attempted_solution=_short(attempted, 600), outcome=outcome, reference_kind="none", @@ -213,6 +214,7 @@ def heuristic_mine( tags=tags, source_sessions=[d.session_id], skill_hint=session_skill_hint(d), + context_by_session={d.session_id: context} if context else {}, ) ) if len(tasks) >= max_tasks: @@ -230,6 +232,7 @@ def dedup_tasks(tasks: List[TaskRecord]) -> List[TaskRecord]: if t.id in by_id: ex = by_id[t.id] ex.source_sessions = list(dict.fromkeys(ex.source_sessions + t.source_sessions)) + ex.context_by_session = ex.context_by_session | t.context_by_session # prefer a resolved outcome if either session resolved it order = {"success": 3, "fail": 2, "mixed": 1, "unknown": 0} if order.get(t.outcome, 0) > order.get(ex.outcome, 0): diff --git a/skillopt_sleep/types.py b/skillopt_sleep/types.py index d5c053d5a..c3bdf07e6 100644 --- a/skillopt_sleep/types.py +++ b/skillopt_sleep/types.py @@ -82,8 +82,10 @@ class TaskRecord: derived_from: str = "" # for dream tasks: the real task id it varies # Which skill this task exercised, when the source session invoked exactly # one. Empty means unknown or ambiguous, which keeps the task in the - # existing managed-skill catch-all path. Kept last for positional compatibility. + # existing managed-skill catch-all path. skill_hint: str = "" + # Bounded excerpts from individual sessions for review. Replay uses context_excerpt. + context_by_session: Dict[str, str] = field(default_factory=dict) def to_dict(self) -> Dict[str, Any]: return asdict(self) diff --git a/tests/PREFERENCE_SCOPE_MATRIX_154.md b/tests/PREFERENCE_SCOPE_MATRIX_154.md index 46fcb3d38..8c7ab489d 100644 --- a/tests/PREFERENCE_SCOPE_MATRIX_154.md +++ b/tests/PREFERENCE_SCOPE_MATRIX_154.md @@ -11,6 +11,6 @@ Synthetic fixtures, not user data or human preference labels. | Ambiguous | uncertainty and absence of durable consent | withhold pending clarification | The tests call the real prompt constructor, heuristic miner, and public mine/dedup path. They make no model calls and do not mock a successful semantic inference. -Four passing prompt checks and four individual-record checks are expected. Four strict xfails express the proposed retention contract: same-intent dedup currently merges source IDs, but not the second record's scope evidence. +Four prompt checks and four individual-record checks cover the evidence before deduplication. Four task-file round-trip checks verify that same-intent deduplication keeps both session IDs and their excerpts in `context_by_session`. Replay still uses the first record's `context_excerpt`. -A task surviving mining is not proof of a skill update. The current matrix detects evidence loss only. Persistence-policy labels above are expectations for discussion, not code outputs. Clean-context replay and model-level preference inference are outside this patch. No production code is changed. +The matrix checks evidence retention through mining and serialization. The persistence-policy labels above describe proposed uses of that evidence.