raglab.memory#
Purpose-indexed memory: what past runs found, kept per purpose (raglab#6, stage 0).
The retrieval index is purpose-free by design; the search agent is the one layer that
sees purpose (the sub-task goal, the refinement chain, the evaluator’s verdicts).
Without memory, everything a run learned dies when it returns. A PurposeStore
keeps the precipitate: the hits each run committed to, as ExtractionUnit s
keyed by purpose, carrying their provenance ((source, artifact_id) plus the whole
SearchHit.to_dict(), raw magnitudes only).
It is a RunLog sink (writes come from the run-log’s
RoundRecord s, the one shared record shape) and a
Memory the agent reads from before sweeping:
store = PurposeStore()
agent = make_search_agent(sources, memory=store)
agent("invoices from 2023") # sweeps, journals what it kept
agent("invoices from 2023") # recalls those hits; sweeps only if not sufficient
Progressive disclosure: store[purpose] is the related-purpose read. With no
embedder only the same purpose (normalized text) is related; inject one
(str -> vector, e.g. a vd/ir embedder) and similar purposes share their findings.
The four verbs of the design (report 14) are injected strategies; stage 0 ships two
(write_strategy: ADD, or UPDATE an artifact already kept; read_strategy:
relevance x importance x recency). CONSOLIDATE and DECAY are follow-ups, gated on
observed need.
Functions
|
|
|
A |
|
The canonical text of a purpose: case-folded, whitespace collapsed. |
|
Content-hash id of a purpose (of its |
|
|
|
ADD new artifacts; UPDATE (replace with the newer unit) those already kept. |
Classes
|
One piece of evidence a run committed to, for one purpose. |
|
|
|
Rank |
|
Merge |
- class raglab.memory.ExtractionUnit(purpose, hit, recorded_at, importance=1.0, confidence=None, level='raw', supersedes=None)[source]#
Bases:
objectOne piece of evidence a run committed to, for one purpose.
hitis theSearchHit.to_dict()of the kept hit: provenance (source,artifact_id) and its raw magnitude, never a fused score.levelis the consolidation level ("raw"until CONSOLIDATE exists);supersedesnames the artifact id a later unit replaces (for SUPERSEDE).
- class raglab.memory.PurposeStore(backend=dict(), embedder=None, write_strategy=<function upsert_write>, read_strategy=<function make_relevance_recency_reader.<locals>.read>, min_relevance=0.8, clock=<built-in function time>)[source]#
Bases:
MutableMappingMutableMapping[purpose, list[ExtractionUnit]]over an injectedbackend.backendmapspurpose_id()to a JSON-clean record{"purpose", "embedding", "units"}: anyMutableMapping(a dict, a dol store of JSON files, …).store[purpose]recalls from every related purpose (the same one, or one whose embedding is at leastmin_relevancecosine-similar);store[purpose] = unitsreplaces that purpose’s units. Calling the store with aRoundRecordjournals what the round kept.- clock()#
time() -> floating-point number
Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.
- recall(purpose)[source]#
The units of every purpose related to
purpose, ranked byread_strategy.- Return type:
- recall_hits(purpose)[source]#
recall()asSearchHits, ready to seed the agent’s pool.- Return type:
list[SearchHit]
- write_strategy(new)#
ADD new artifacts; UPDATE (replace with the newer unit) those already kept.
- Return type:
- class raglab.memory.ReadStrategy(*args, **kwargs)[source]#
Bases:
ProtocolRank
(relevance, unit)candidates (from related purposes) into what to recall.
- class raglab.memory.WriteStrategy(*args, **kwargs)[source]#
Bases:
ProtocolMerge
newunits into a purpose’sexistingones (ADD/UPDATE/SUPERSEDE/NOOP).
- raglab.memory.json_clean(obj)[source]#
objas plain JSON types (numpy values viatolist, the rest viastr).>>> json_clean({"p": (1, 2), "x": {1}}) {'p': [1, 2], 'x': '{1}'}
- Return type:
- raglab.memory.make_relevance_recency_reader(*, half_life=2592000.0, alpha=1.0)[source]#
A
ReadStrategyscoringrelevance**alpha * importance * recency.recencyhalves everyhalf_lifeseconds. Each artifact is recalled once, at its best score, best first.- Return type:
- raglab.memory.normalize_purpose(statement)[source]#
The canonical text of a purpose: case-folded, whitespace collapsed.
>>> normalize_purpose(" Invoices from 2023 ") 'invoices from 2023'
- Return type:
- raglab.memory.purpose_id(statement)[source]#
Content-hash id of a purpose (of its
normalize_purpose()text).>>> purpose_id("Invoices from 2023") == purpose_id("invoices from 2023") True
- Return type: