File size: 7,894 Bytes
6c5f29f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """Deterministic OracleMem-Small instance generator."""
from __future__ import annotations
import random
from .schema import CandidateMemory, EvidenceUnit, Experience, Instance, Query
PREFERENCES = [
("coffee", "prefers pour-over coffee"),
("seat", "prefers aisle seats"),
("food", "is vegetarian"),
("music", "likes ambient music while working"),
("hotel", "prefers quiet hotels"),
("editor", "uses VS Code"),
("language", "prefers Python examples"),
("meeting", "prefers morning meetings"),
]
UPDATED_VALUES = {
"coffee": "prefers green tea",
"seat": "prefers window seats",
"food": "is no longer vegetarian",
"music": "prefers silence while working",
"hotel": "prefers hotels near transit",
"editor": "uses Neovim",
"language": "prefers Rust examples",
"meeting": "prefers afternoon meetings",
}
TOOL_FACTS = [
("indexer", "build_index skips tombstoned memories before embedding"),
("calendar", "schedule_sync retries failed calendar writes once"),
("tests", "test_memory_budget checks one representation per experience"),
("retriever", "search filters deleted memories before reranking"),
]
def _cost(text: str) -> int:
return max(1, len(text.split()))
def _candidate(
experience_id: str,
representation: str,
text: str,
coverage: dict[str, float],
generator: str = "oracle",
) -> CandidateMemory:
return CandidateMemory(
candidate_id=f"{experience_id}:{representation}",
experience_id=experience_id,
representation=representation,
text=text,
cost=_cost(text),
coverage={unit: float(value) for unit, value in coverage.items() if value > 0},
generator=generator,
)
def generate_instance(
seed: int = 0,
num_base_facts: int = 6,
num_updates: int = 3,
num_tool_facts: int = 2,
) -> Instance:
"""Generate one exact OracleMem-Small synthetic instance.
The visible experience stream contains no query ids. Held-out queries are
generated only after units and candidates exist, which keeps write-time
policies from seeing future evidence requirements.
"""
rng = random.Random(seed)
facts = rng.sample(PREFERENCES, k=min(num_base_facts, len(PREFERENCES)))
update_keys = [key for key, _ in facts if key in UPDATED_VALUES]
update_keys = rng.sample(update_keys, k=min(num_updates, len(update_keys)))
tool_facts = rng.sample(TOOL_FACTS, k=min(num_tool_facts, len(TOOL_FACTS)))
units: list[EvidenceUnit] = []
experiences: list[Experience] = []
candidates: list[CandidateMemory] = []
queries: list[Query] = []
current_unit_for_key: dict[str, str] = {}
invalidation_for_key: dict[str, str] = {}
timestamp = 0
for key, text in facts:
unit_id = f"unit:{key}:initial"
prop_id = f"prop:{key}"
units.append(EvidenceUnit(unit_id, "preference", text, prop_id, timestamp))
exp_id = f"exp:{timestamp:04d}"
visible = (unit_id,)
exp_text = f"User says: My {key} preference is that I {text}."
experiences.append(Experience(exp_id, f"session:{timestamp // 4}", timestamp, exp_text, visible))
candidates.extend([
_candidate(exp_id, "raw", exp_text, {unit_id: 1.0}),
_candidate(exp_id, "fact", f"FACT {prop_id}: user {text}.", {unit_id: 1.0}),
_candidate(exp_id, "summary", f"SUMMARY: user {text}.", {unit_id: 0.6}),
])
current_unit_for_key[key] = unit_id
timestamp += 1
for key in update_keys:
old_unit = current_unit_for_key[key]
new_text = UPDATED_VALUES[key]
current_id = f"unit:{key}:current"
invalid_id = f"unit:{key}:invalidates_initial"
prop_id = f"prop:{key}"
units.append(EvidenceUnit(current_id, "current_preference", new_text, prop_id, timestamp))
units.append(EvidenceUnit(
invalid_id,
"invalidation",
f"{prop_id} initial value is no longer current",
prop_id,
timestamp,
state="superseded",
metadata={"invalidates": old_unit, "replacement": current_id},
))
exp_id = f"exp:{timestamp:04d}"
visible = (current_id, invalid_id)
exp_text = f"User correction: Actually, for {key}, I changed my mind and now {new_text}."
experiences.append(Experience(exp_id, f"session:{timestamp // 4}", timestamp, exp_text, visible))
candidates.extend([
_candidate(exp_id, "raw", exp_text, {current_id: 1.0, invalid_id: 1.0}),
_candidate(exp_id, "fact", f"FACT {prop_id}: user now {new_text}.", {current_id: 1.0}),
_candidate(exp_id, "tombstone", f"TOMBSTONE {prop_id}: initial value invalid after t={timestamp}.", {invalid_id: 1.0}),
_candidate(
exp_id,
"compound_update",
f"UPDATE {prop_id}: user now {new_text}; tombstone previous value after t={timestamp}.",
{current_id: 1.0, invalid_id: 1.0},
),
_candidate(exp_id, "summary", f"SUMMARY: {key} changed; current state is {new_text}.", {current_id: 0.7, invalid_id: 0.5}),
])
current_unit_for_key[key] = current_id
invalidation_for_key[key] = invalid_id
timestamp += 1
for key, text in tool_facts:
unit_id = f"unit:tool:{key}"
prop_id = f"prop:tool:{key}"
units.append(EvidenceUnit(unit_id, "tool_outcome", text, prop_id, timestamp))
exp_id = f"exp:{timestamp:04d}"
exp_text = f"Tool trace: after debugging {key}, the result is that {text}."
experiences.append(Experience(exp_id, f"session:{timestamp // 4}", timestamp, exp_text, (unit_id,)))
candidates.extend([
_candidate(exp_id, "raw", exp_text, {unit_id: 1.0}),
_candidate(exp_id, "graph_edge", f"EDGE tool:{key} outcome '{text}'.", {unit_id: 1.0}),
_candidate(exp_id, "skill", f"SKILL: when handling {key}, remember that {text}.", {unit_id: 0.9}),
_candidate(exp_id, "summary", f"SUMMARY: {key} outcome: {text}.", {unit_id: 0.6}),
])
timestamp += 1
query_id = 0
for key, unit_id in current_unit_for_key.items():
unit = next(item for item in units if item.unit_id == unit_id)
required = [unit_id]
if key in invalidation_for_key:
required.append(invalidation_for_key[key])
category = "current_truth_update"
text = f"What is the user's current {key} preference?"
else:
category = "single_hop_fact"
text = f"What did the user say about {key}?"
queries.append(Query(
f"query:{query_id:04d}",
text,
category,
tuple(required),
unit.text,
))
query_id += 1
for key in invalidation_for_key:
queries.append(Query(
f"query:{query_id:04d}",
f"Is the user's initial {key} preference still current?",
"deletion_or_supersession",
(invalidation_for_key[key],),
"No, it was superseded.",
))
query_id += 1
for key, _ in tool_facts:
unit_id = f"unit:tool:{key}"
unit = next(item for item in units if item.unit_id == unit_id)
queries.append(Query(
f"query:{query_id:04d}",
f"What should the agent remember about {key}?",
"tool_outcome",
(unit_id,),
unit.text,
))
query_id += 1
queries.append(Query(
f"query:{query_id:04d}",
"What is the user's favorite vacation island?",
"abstention",
tuple(),
"Insufficient evidence.",
))
return Instance(
instance_id=f"oraclemem-small-{seed}",
seed=seed,
units=tuple(units),
experiences=tuple(experiences),
candidates=tuple(candidates),
queries=tuple(queries),
metadata={
"num_base_facts": num_base_facts,
"num_updates": num_updates,
"num_tool_facts": num_tool_facts,
},
)
|