Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion skillopt_sleep/mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -205,14 +206,15 @@ 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",
reference="",
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:
Expand All @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion skillopt_sleep/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/PREFERENCE_SCOPE_MATRIX_154.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
25 changes: 12 additions & 13 deletions tests/test_preference_scope_contracts.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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